Parse two files and merge lines into one file using bash

I have two text files that I would like to parse, and then combine the two lines parsed into a single file using a call grep. For instance:

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 trying to use the grep command as such

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

but I'm not sure how grep both files are.

I would suggest using the command to concatenate paste, but I'm not sure.

Also, if this is hard to understand, let me know so that I can clarify.

Thank!

+4
source share
1 answer

Reply to the revised question

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

Answer to the original question

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

How it works

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

    . -d '' .

  • grep '^\*'

    , *.

    -v, : grep -v '^\*' , *.

+5

Source: https://habr.com/ru/post/1658265/


All Articles