【问题标题】:linux shell diff two files to get new linelinux shell 比较两个文件以获取新行
【发布时间】:2017-08-11 05:38:16
【问题描述】:

我有两个文件,我想通过比较两个文件来获取新行,我知道可以使用 'diff newfile oldfile' 来获取新行,但输出将包括“

例如,现在我有一个旧文件:

a
b
c

还有一个新文件

a
b
c
d
e
f

'diff newfile oldfile'的结果将是

4,6d3
< d
< e
< f

但我想要的结果是

d
e
f

那么我怎样才能得到这个输出呢?我搜索了很多差异选项,但没有任何想法

提前谢谢你。

【问题讨论】:

标签: shell diff


【解决方案1】:

类似于this question,你可以使用comm来达到这个目的。

comm -13 file1 file2

将仅打印file2 中不存在于file1 中的行。

【讨论】:

  • @SakuraKyouko 只要记住comm 需要对输入文件进行排序。
【解决方案2】:

原生diff解决方案:

diff --changed-group-format='%<' --unchanged-group-format='' new.txt old.txt

输出:

d
e
f

【讨论】:

    【解决方案3】:

    你也可以使用 awk:

    $ awk 'NR==FNR{a[$0];next} ($0 in a==0)' oldfile newfile
    d
    e
    f
    

    grep,如果文件不是那么大(注意部分匹配):

    $ grep -v -f oldfile newfile
    d
    e
    f
    

    join(输入文件需要订购):

    $ join -v 2 oldfile newfile
    d
    e
    f
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2015-04-12
      • 1970-01-01
      • 2011-06-05
      • 2013-01-08
      • 1970-01-01
      相关资源
      最近更新 更多