Linux 如何做相反的差异?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8581090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:44:50  来源:igfitidea点击:

How to do the opposite of diff?

linuxshelldiff

提问by Justin Yoder

Possible Duplicate:
how to show lines in common (reverse diff)?

可能的重复:
如何显示共同的行(反向差异)?

Is there a command to do the opposite of diff? I want to compare two files if the same thing exists in both create a list of them. i am trying to figure out what entry's exist in both files.

是否有命令可以执行与 diff 相反的操作?如果两个文件中存在相同的东西,我想比较两个文件,创建它们的列表。我想弄清楚这两个文件中都存在哪些条目。

回答by Frank Schmitt

Use the join command:

使用连接命令:

join a.txt b.txt

join a.txt b.txt

assuming the files are sorted; if not:

假设文件已排序;如果不:

sort a.txt > sorted_a.txt; sort b.txt > sorted_b.txt; join sorted_a.txt sorted_b.txt

回答by fge

Here is a solution that WILL NOT change the order of the lines:

这是一个不会改变行顺序的解决方案:

fgrep -x -f file1 file2