Reputation: 3125
I'm trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1. If not found, print Column1 of File1 anyways.
$ File1
Harvard Basketball
Yale Baseball
Princeton Football
Stanford Tennis
$ File2
Rutgers 10 people
Stanford 20 people
Yale 32 people
Tufts 43 people
Harvard 51 people
Desired Output:
Harvard 51 people
Yale 32 people
Princeton
Stanford 20 people
I tried the following code:
awk 'NR==FNR{A[$1]=$2;next} A[$1]{print}' file1 file2
However this doesn't give me Princeton
when column2 is empty, it's omitted entirely. Any help?
Upvotes: 1
Views: 207
Reputation: 77075
Here is another way using join(1) command:
$ join -a 1 -o 1.1 2.2 2.3 <(sort file1) <(sort file2)
Harvard 51 people
Princeton
Stanford 20 people
Yale 32 people
Upvotes: 2
Reputation: 23374
you need to switch things around a bit
awk 'NR==FNR{A[$1]=$2FS$3;next} {print $1,A[$1]}' file2 file1
Harvard 51 people
Yale 32 people
Princeton
Stanford 20 people
Upvotes: 1