linux shell 比较两个文件以获得新行

linux shell diff two files to get new line

我有两个文件,我想通过比较两个文件来获取新行,我知道可以使用 'diff newfile oldfile' 来获取新行,但输出将包括“<”和差异信息,我不想拥有。

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

a
b
c

和一个新文件

a
b
c
d
e
f

'diff newfile oldfile' 的结果将是

4,6d3
< d
< e
< f

但我想要的结果是

d
e
f

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

提前致谢。

this question, you can use comm 类似。

comm -13 file1 file2

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

你也可以使用 awk:

$ awk 'NR==FNR{a[[=10=]];next} ([=10=] 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

原生差异解决方案:

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

输出:

d
e
f