【发布时间】:2018-12-13 07:33:36
【问题描述】:
我正在尝试使用来自 awk compare columns from two files, impute values of another column 的类似命令,并查看了与我的类似的各种问题 awk search column from one file, if match print columns from both files , How to import fields in other columns corresponding to one common field in two files with `NA` in all unmatched columns , awk compare 2 files, 2 fields different order in the file, print or merge match and non match lines 使用具有更多字段但我无法使其工作的文件。我还阅读了http://theunixshell.blogspot.com/2012/12/i-have-two-files-file-1-contains-3.html 的内容,看看它是否可行,但我仍然遇到问题:
文件 1:
xx NC1 12 13 ! pro
xy NC1 15 17 ! pro
yx NC1 18 20 ! pro
yy NC1 22 28 ! pro
文件 2
xx ds
xy jt
yy wp
想要的输出:
xx NC1 12 13 ! pro ds
xy NC1 15 17 ! pro jt
yx NC1 18 20 ! pro NA
yy NC1 22 28 ! pro wp
我正在使用的代码:
awk 'NR==FNR { a[$1]=$6; next }{print $0 " " ($2 in a ? a[$2] : "NA")}' file2 file1
所以基本上我的输出给了我一个新列,都是“NA”,这显然不是我想要达到的。
输出:
xx NC1 12 13 ! pro NA
xy NC1 15 17 ! pro NA
yx NC1 18 20 ! pro NA
yy NC1 22 28 ! pro NA
【问题讨论】: