1

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.

user3299633
  • 2,971
  • 3
  • 24
  • 38
  • Possible duplicate of [How to merge two files using AWK?](http://stackoverflow.com/questions/5467690/how-to-merge-two-files-using-awk) – Srini V Jul 05 '16 at 21:45

2 Answers2

2

It's a job for join:

join -a1 -o 1.1 2.1 2.2 file1 file2
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • This is the better solution. Using `awk` is like reinventing the wheels. Or using a general purpose computer (awk) for an embedded controller (join). – alvits Jul 06 '16 at 00:55
  • @alvits: Perhaps for this case, but the awk solution is also able to deal with non-sorted files and doesn't need a mask (that can be useful if the second file has more fields). – Casimir et Hippolyte Jul 06 '16 at 01:17
1

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
anubhava
  • 761,203
  • 64
  • 569
  • 643