4

I have two text files which I would like to parse through, then combine the two lines parsed into one file using a grep call. For example:

Foo.txt                  Foo2.txt
*1                        A
 2                        B
*3                        C
 4                        D
*5                        E

I would like to have

FooFinal.txt
 2B
 4D

I am attempting to use the grep command as so

grep -A 1 '*' Foo.txt|grep -v '*'

but, I am not sure how to grep both files.

I would assume one would use a paste command to concatenate, but I am not sure.

Also, if this is difficult to understand, please let me know so I can clarify.

Thanks!

John1024
  • 109,961
  • 14
  • 137
  • 171
branch.lizard
  • 595
  • 1
  • 5
  • 15

1 Answers1

5

Answer for revised question

$ paste -d '' foo.txt foo2.txt | grep -v '^\*'
 2B
 4D

Answer for original question

$ paste -d '' foo.txt foo2.txt | grep '^\*'
*1A
*3C
*5E

How it works

  • paste -d '' foo.txt foo2.txt

    This combines the two files line by line. The option -d '' tells paste to combine the lines directly with no intervening whitespace.

  • grep '^\*'

    This selects only those lines which begin with *.

    For the revised question, we add -v which inverts the match: grep -v '^\*' removes the lines that start with *.

John1024
  • 109,961
  • 14
  • 137
  • 171