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

标签 linux file awk grep console

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

相关文章:

linux - 将参数以 txt 文件的形式传递到 shell 脚本中

linux - Centos需要很长时间才能解析本地网络附近的服务器

c# - 图像文件副本,正在被另一个进程使用

file - 将文件名中具有特定字符串的目录中的所有文件复制到 Bash 中的不同目录

linux - 使用 awk 打印格式化输出

awk - 是否可以漂亮地打印 Awk 的代码?

bash - awk 中的 OFS 问题

c++ - 无限递归变成D模式过程

linux - chmod 777/的效果。 -R 带根

java - 无法将消息写入控制台中的 Excel 文件,显示 java.lang.NullPointerException