user3299633 user3299633 - 5 months ago 12
Linux Question

Combine Two Files With Common Column

I have two files that look like the following

First File:

FileA
FileB
FileC


Second File:

FileA 2
FileC 2


I want the third file to look like the following:

FileA FileA 2
FileB
FileC FileC 2


Basically I'm doing a selective paste. I'm open to any awk or sed solution in order to achieve the desired results.

Answer

Using awk you can do:

awk 'FNR == NR{a[$1]=$0; next} {print $0, a[$1]}' file2 file1

FileA FileA 2
FileB
FileC FileC 2