简体   繁体   English

查找两个文件之间的差异

[英]Finding the differences between two files

I have a couple of packages lists, with hundreds of entries, and I would like to find the differences of them. 我有几个软件包列表,其中包含数百个条目,我想找到它们之间的区别。

The contents of the files look like 文件内容看起来像

File 1: 文件1:

somepackage1 0.1
somepackage2 5.6
somepackage3 1.3-1
etc...

File 2: 档案2:

somepackage1 0.1
somepackage2 5.7
somepackage3 1.3-1
somepackage4 0.1
etc...

I'm looking for a couple of commands or a script that can produce all the new packages that were added. 我正在寻找可以产生所有新添加的软件包的命令或脚本。 Not just the version numbers, but if a new package was added. 不仅是版本号,还包括是否添加了新软件包。

So, for example the command or script would output somepackage4 given the above two files. 因此,例如,给定上述两个文件,命令或脚本将输出somepackage4

I've been playing with some commands, but can't get any of them work properly. 我一直在玩一些命令,但其中任何一个都无法正常工作。 Does anyone have a good way of doing this? 有人有这样做的好方法吗?

You seem to want to compare the first column in the files and print the lines that are unique in the second one. 您似乎想比较文件中的第一列,并打印第二列中唯一的行。 Use comm : 使用comm

comm -13 <(awk '{print $1}' file1 | sort) <(awk '{print $1}' file2 | sort)

For your input, it'd produce: 对于您的输入,它将产生:

somepackage4

you can use grep 你可以使用grep
to find packages in listed in file2 but not in file1 查找文件2中列出但文件1中未列出的软件包

grep -vf <(cut -d' ' -f1 file1)  <(cut -d' ' -f1 file2)

使用awk

awk 'NR==FNR{a[$1];next}!($1 in a){print $1}' file1 file2

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

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