spr*_*aff 12 unix bash perl diff text
我有时需要比较两个文本文件.显然,diff
显示出差异,它也隐藏了相似之处,这是一个重点.
假设我想对这些文件进行其他比较:set union,intersection和subtraction,将每一行视为集合中的元素.
是否有类似简单的常用工具或单行可以做到这一点?
例子:
A.TXT
john
mary
Run Code Online (Sandbox Code Playgroud)
b.txt
adam
john
Run Code Online (Sandbox Code Playgroud)
$> set_union a.txt b.txt
john
mary
adam
Run Code Online (Sandbox Code Playgroud)
$> set_intersection a.txt b.txt
john
Run Code Online (Sandbox Code Playgroud)
$> set_difference a.txt b.txt
mary
Run Code Online (Sandbox Code Playgroud)
如果要获取两个文件之间的公共线,可以使用comm实用程序.
A.txt:
A
B
C
Run Code Online (Sandbox Code Playgroud)
B.txt
A
B
D
Run Code Online (Sandbox Code Playgroud)
然后,使用comm会给你:
$ comm <(sort A.txt) <(sort B.txt)
A
B
C
D
Run Code Online (Sandbox Code Playgroud)
在第一列中,您拥有第一个文件中的内容而不是第二个文件中的内容.
在第二列中,您拥有第二个文件中的内容,而不是第一个文件中的内容.
在第三列中,您拥有两个文件中的内容.