繁体   English   中英

比较两个文件中的列并打印不匹配

[英]Compare columns from two files and print not match

我想比较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中并被打印。

暂无
暂无

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

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