如何在单独的文件中使用 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
一个.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 列(出现在两个文件中的行)
假设您只想保留文件 2 独有的行,您可以执行以下操作:
comm -13 file1 file2
请注意,该comm
命令要求这两个文件按排序顺序排列。
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 ituniq -u
prints the unique lines (that do not repeat). It requires the input to be pre-sorted.使用组格式说明符,您可以禁止打印未更改的行并仅打印更改的行以进行更改
diff --changed-group-format="%>" --unchanged-group-format="" file1 file2