我想比较 file1 和 file2 的前 4 列。我想打印 file1 中的所有行 + file2 中不在 file1 中的行。
File1:
2435 2 2 7 specification 9-8-3-0
57234 1 6 4 description 0-0 55211
32423 2 44 3 description 0-0 24242
File2:
2435 2 2 7 specification
7624 2 2 1 namecomplete
57234 1 6 4 description
28748 34 5 21 gateway
32423 2 44 3 description
832758 3 6 namecomplete
output:
2435 2 2 7 specification 9-8-3-0
57234 1 6 4 description 0-0 55211
32423 2 44 3 description 0-0 24242
7624 2 2 1 namecomplete
28748 34 5 21 gateway
832758 3 6 namecomplete
我不明白如何打印不匹配的东西。
最佳答案
您可以使用这样的 awk 脚本来完成:
script.awk
FNR == NR { mem[ $1 $2 $3 $4 $5 ] = 1;
print
next
}
{ key = $1 $2 $3 $4 $5
if( ! ( key in mem) ) print
}
然后像这样运行它:awk -f script.awk file1 file2
。
第一部分内存前 5 个字段,打印整行并移动到下一行。这部分专门应用于第一个文件中的行。
第二部分仅应用于第二个文件中的行。它检查该行是否不在 mem
中,如果是,则该行不在 file1 中并打印出来。
关于linux - 比较两个文件中的列并打印不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37953065/