【问题标题】:Compare columns from two files and print not match比较两个文件中的列并打印不匹配
【发布时间】:2016-10-23 12:17:19
【问题描述】:

我想比较 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

我不明白如何打印不匹配的内容。

【问题讨论】:

    标签: linux file awk grep console


    【解决方案1】:

    您可以使用这样的 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 中并被打印。

    【讨论】:

    • "if( ! ( key in mem) ) print" 如何更改此打印以便更改行?
    • @diborbi 就像print $1 $3 "whatever" 一样写下来。 print 是快捷方式,意思是print $0
    猜你喜欢
    • 2017-07-30
    • 2020-11-24
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多