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

Ste*_*eam 19 unix linux bash

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

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

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

我需要知道找到所有差异的最快方法,并将其列在下面每个案例的文件中 -

Case 1 - file2 = file1 + extra text appended.
Case 2 - file2 and file1 are different.
Run Code Online (Sandbox Code Playgroud)

dan*_*nmc 46

你可以试试..

comm -13 <(sort file1) <(sort file2) > file3
Run Code Online (Sandbox Code Playgroud)

要么

grep -Fxvf file1 file2 > file3
Run Code Online (Sandbox Code Playgroud)

要么

diff file1 file2 | grep "<" | sed 's/^<//g'  > file3
Run Code Online (Sandbox Code Playgroud)

要么

join -v 2 <(sort file1) <(sort file2) > file3
Run Code Online (Sandbox Code Playgroud)

  • 使用两个大文本文件,其中一个文本在开头附近有一段额外的文本,我计时所有四种方法.grep,diff和join方法都无法找到额外的段落.除了"<"之外,diff方法还需要grep">"才能工作.我不熟悉grep或join方法.结果:comm:3.661s,grep:0.035s,diff:0.051s,join:3.811s (2认同)

pro*_*ron 14

另外一个选项:

sort file1 file2 | uniq -u > file3
Run Code Online (Sandbox Code Playgroud)

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

sort file1 file2 | uniq -d > file3
Run Code Online (Sandbox Code Playgroud)