说我有两个文件:a.txt
和b.txt
.
内容a.txt
:
hello world
Run Code Online (Sandbox Code Playgroud)
内容b.txt
:
hello world
something else
Run Code Online (Sandbox Code Playgroud)
当然我可以用它vimdiff
来检查它们的区别,我可以确保它a.txt
是 的一个子集b.txt
,这意味着它b.txt
必须包含 中存在的所有行a.txt
(就像上面的例子一样)。
我的问题是如何将存在b.txt
但不存在的行记录a.txt
到文件中?
cas*_*cas 14
comm -1 -3 a.txt b.txt > c.txt
Run Code Online (Sandbox Code Playgroud)
在-1
排除线路是只在a.txt
,而且-3
是在这两个排除线。因此,仅b.txt
输出排他性的行(请参阅man comm
或comm --help
了解详细信息)。输出被重定向到c.txt
如果您想要两个文件之间的差异,请使用diff
而不是comm
. 例如
diff -u a.txt b.txt > c.txt
Run Code Online (Sandbox Code Playgroud)
如果你不关心子集,你可以只使用
diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
Run Code Online (Sandbox Code Playgroud)
.
$ cat a.txt
hello world
$ cat b.txt
hello world
something else
$ diff a.txt b.txt|grep ">"|cut -c 3- > foo.txt
$ cat foo.txt
something else
Run Code Online (Sandbox Code Playgroud)