用awk比较两个文件并输出不匹配的行 [英] Comparing two files with awk and output non matching lines
本文介绍了用awk比较两个文件并输出不匹配的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个具有不均匀列的文件,我想比较这两个文件并从file1中删除匹配的行
I have two files with uneven columns and I want to compare these two files and remove the matching line from file1
文件1:
nt1 ID420
nt1 ID42
nt56 ID6008
ht44 ID555
lt12 ID34
lt12 ID3434
ntt56 ID667
klll ID87693
file2
nt23 ID42
ht44 ID555
lt12 ID3434
期望的输出:
nt1 ID420
nt56 ID6008
lt12 ID34
ntt56 ID667
klll ID87693
我在较早的文章中使用了以下命令,但结果不完全匹配,并且需要使用初始字符进行匹配.我在awk命令上没有太多exp,并且我需要awk单行命令或sed或类似的shell脚本来获取此输出.提前谢谢:
I have used below command from earlier post but result is not exactly matching and that is taking initial character for matching. I do not have much exp on awk commands and I need awk single line command or sed or similar shell script to get this output. Thank you well in advance:
awk -FS=" " 'NR==FNR {b[$0]; next} {for (x in b) if($0 ~ x) next;print $0}' file2 file1 > outputfile
推荐答案
这将是最快的:
grep -vFxf file2 file1
使用awk:
awk 'NR==FNR {exclude[$0];next} !($0 in exclude)' file2 file1
如果您不在乎输出是否已排序,则 comm
的作用是:
If you don't care that the output is sorted, this is what comm
is for:
comm -23 <(sort file1) <(sort file2)
这篇关于用awk比较两个文件并输出不匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文