繁体   English   中英

awk中的Vlookup:如何在输出末尾列出在file2中但不在file1中发生的所有事件?

[英]Vlookup in awk: how to list anything occuring in file2 but not in file1 at the end of output?

我有两个文件,文件1:

1    800     800     0.51
2    801     801     0.01
3    802     802     0.01
4    803     803     0.23

和文件2:

1    800     800     0.55
2    801     801     0.09
3    802     802     0.88
4    804     804     0.24

我有一个awk脚本,该脚本在第二个文件中查找与第一个文件的前三列匹配的值。

$ awk 'NR==FNR{a[$1,$2,$3];next} {if (($1,$2,$3) in a) {print $4} else {print "not found"}}' f1 f2
0.55
0.09
0.88
not found

有没有一种方法可以使匹配后的文件2中出现的,不在文件1中的任何行仍添加到输出的末尾,例如:

0.55
0.09
0.88
not found
4    804     804     0.24

这样,当我将两个文件粘贴回一起时,它们将如下所示:

1 800 800 0.51 0.55
2 801 801 0.01 0.09
3 802 802 0.01 0.88
4 803 803 0.23 not found
4 804 804 not found 0.04

还是有其他语法完全不同的更优雅的解决方案?

 awk '{k=$1FS$2FS$3}NR==FNR{a[k]=$4;next}
      k in a{print $4;next}{print "not found";print}' f1 f2

以上一线将为您提供:

0.55
0.09
0.88
not found
4 804 804 0.24

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM