linux - 具有不包含额外行的输出的 diff 2 文件

标签 linux bash unix diff

我有 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/

相关文章:

php - 将 Unix 时间转换为日期和时间,无需使用任何库或预定义的帮助程序

c - GCC错误但VS没有错误

linux - 如何提取grep并切入bash数组

c++ - 如何从输入文件中添加和删除用户到linux系统?

linux - 检查域是否在服务器上处于事件状态

linux - bash 提示不显示行首

bash - cat `bash` 命令的意外输出

unix - 在 Jenkins 上配置 cron 作业每 15 分钟运行一次

linux - 如何生成.so文件

c++ - 检查信号处理程序是否不是默认的