24

如何在单独的文件中使用 diff 仅显示不同的行?

例如,文件编号 1 包含以下行:

1;john;125;3
1;tom;56;2
2;jack;10;5

文件编号 2 包含以下行:

1;john;125;3
1;tom;58;2
2;jack;10;5

如何使以下发生?

1;tom;58;2
4

4 回答 4

37

一个.txt:

1;john;125;3
1;tom;56;2
2;jack;10;5

b.txt:

1;john;125;3
1;tom;58;2
2;jack;10;5

使用通讯

comm -13 a.txt b.txt 
1;tom;58;2

命令行选项comm非常简单:

-1 抑制第 1 列(FILE1 独有的行)

-2 抑制第 2 列(FILE2 独有的行)

-3 抑制第 3 列(出现在两个文件中的行)

于 2012-04-30T07:14:59.837 回答
13

假设您只想保留文件 2 独有的行,您可以执行以下操作:

comm -13 file1 file2

请注意,该comm命令要求这两个文件按排序顺序排列。

于 2012-04-30T07:15:32.237 回答
12

Here's a simple solution that I think is better than diff:

sort file1 file2 | uniq -u

  • sort file1 file2 concatenates the two files and sorts it
  • uniq -u prints the unique lines (that do not repeat). It requires the input to be pre-sorted.
于 2019-08-28T22:19:20.473 回答
7

使用组格式说明符,您可以禁止打印未更改的行并仅打印更改的行以进行更改

diff --changed-group-format="%>" --unchanged-group-format="" file1 file2

于 2016-07-26T18:29:55.287 回答