Linux 在unix中查找两个文件之间差异的最快方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18069611/
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-07 00:28:31  来源:igfitidea点击:

Fastest way of finding differences between two files in unix?

linuxbashunix

提问by Steam

I want to find the difference between two files and then put only the differences in a third file. I saw different approaches using awk, diff and comm. Are there any more ?

我想找到两个文件之间的差异,然后只将差异放在第三个文件中。我看到了使用 awk、diff 和 comm 的不同方法。还有吗?

eg.Compare two files line by line and generate the difference in another file

例如。逐行比较两个文件并在另一个文件中生成差异

eg.Copy differences between two files in unix

例如。在 unix 中复制两个文件之间的差异

I need to know which is the fastest way of finding all the differences and listing them in a file for each of the cases below -

我需要知道哪种最快的方法可以找到所有差异并将它们列在以下每种情况的文件中 -

Case 1 - file2 = file1 + extra text appended.
Case 2 - file2 and file1 are different.

采纳答案by danmc

You could try..

你可以试试..

comm -13 <(sort file1) <(sort file2) > file3

or

或者

grep -Fxvf file1 file2 > file3

or

或者

diff file1 file2 | grep "<" | sed 's/^<//g'  > file3

or

或者

join -v 2 <(sort file1) <(sort file2) > file3

回答by P_M

You could also try to include md5-hash-sums or similar do determine whether there are any differences at all. Then, only compare files which have different hashes...

您也可以尝试包含 md5-hash-sums 或类似的内容来确定是否存在任何差异。然后,只比较具有不同哈希值的文件......

回答by pron

Another option:

另外一个选项:

sort file1 file2 | uniq -u > file3

If you want to see just the duplicate entries use "uniq -d" option:

如果您只想查看重复的条目,请使用“uniq -d”选项:

sort file1 file2 | uniq -d > file3

回答by James Bond 86

This will work fast:

这将快速工作:

Case 1 - File2 = File1 + extra text appended.

案例 1 - File2 = File1 + 附加的额外文本。

grep -Fxvf File2.txt File1.txt >> File3.txt

grep -Fxvf File2.txt File1.txt >> File3.txt

File 1: 80 Lines File 2: 100 Lines File 3: 20 Lines

文件 1:80 行文件 2:100 行文件 3:20 行