我正在尝试将File1的Column1的值与File2的Column1进行比较。如果找到匹配项,则从File1的Column2打印相应的值。如果未找到,则无论如何都要打印File1的Column1。
$ 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
我尝试了以下代码:
awk 'NR==FNR{A[$1]=$2;next} A[$1]{print}' file1 file2
但是当column2为空时,这不会给我Princeton
,它完全被省略。有什么帮助吗?
答案 0 :(得分:2)
这是使用join(1)命令的另一种方式:
$ join -a 1 -o 1.1 2.2 2.3 <(sort file1) <(sort file2)
Harvard 51 people
Princeton
Stanford 20 people
Yale 32 people
答案 1 :(得分:1)
你需要稍微改变一下
awk 'NR==FNR{A[$1]=$2FS$3;next} {print $1,A[$1]}' file2 file1
Harvard 51 people
Yale 32 people
Princeton
Stanford 20 people