gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com