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,473
  • 2
  • 21
  • 38
  • Possible duplicate of [How to merge two files using AWK?](http://stackoverflow.com/questions/5467690/how-to-merge-two-files-using-awk) – SriniV 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
  • 85,718
  • 5
  • 90
  • 121
  • 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
  • 713,503
  • 59
  • 514
  • 593