说我有两个文件: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)
| 归档时间: |
|
| 查看次数: |
35798 次 |
| 最近记录: |