我有 2 个文件 test 和 test1,我想在它们之间进行比较,但输出没有额外的字符 2a3、4a6、6a9,如下所示。
mangoes
apples
banana
peach
mango
strawberry
测试1:
mangoes
apples
blueberries
banana
peach
blackberries
mango
strawberry
star fruit
当我比较两个文件时
$ diff test test1
2a3
> blueberries
4a6
> blackberries
6a9
> star fruit
如何得到输出结果
$ diff test test1
blueberries
blackberries
star fruit
最佳答案
使用 comm
的解决方案:
comm -13 <(sort test) <(sort test1)
解释
comm - compare two sorted files line by line
With no options, produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files
因为我们只需要第二个文件 test1
特有的行,所以 -13
用于抑制不需要的列。
Process Substitution用于获取排序的文件。
关于linux - 具有不包含额外行的输出的 diff 2 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53287668/