Sor*_*gal 204
diff(1)不是答案,但comm(1)是.
NAME
comm - compare two sorted files line by line
SYNOPSIS
comm [OPTION]... FILE1 FILE2
...
-1 suppress lines unique to FILE1
-2 suppress lines unique to FILE2
-3 suppress lines that appear in both files
Run Code Online (Sandbox Code Playgroud)
所以
comm -2 -3 file1 file2 > file3
Run Code Online (Sandbox Code Playgroud)
必须对输入文件进行排序.如果不是,请先排序.这可以使用临时文件来完成,或者......
comm -2 -3 <(sort file1) <(sort file2) > file3
Run Code Online (Sandbox Code Playgroud)
只要你的shell支持进程替换(bash).
Tha*_*tos 47
Unix实用程序diff
就是为了这个目的.
$ diff -u file1 file2 > file3
Run Code Online (Sandbox Code Playgroud)
有关选项,不同的输出格式等,请参阅手册和Internet.
Nei*_*val 20
考虑一下:
文件a.txt:
abcd
efgh
Run Code Online (Sandbox Code Playgroud)
文件b.txt:
abcd
Run Code Online (Sandbox Code Playgroud)
您可以找到与以下区别:
diff -a --suppress-common-lines -y a.txt b.txt
Run Code Online (Sandbox Code Playgroud)
输出将是:
efgh
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令在输出文件(c.txt)中重新映射输出:
diff -a --suppress-common-lines -y a.txt b.txt > c.txt
Run Code Online (Sandbox Code Playgroud)
这将回答你的问题:
"...包含file1中不存在于file2中的行."
有时diff
是您需要的实用程序,但有时join
更合适.这些文件需要预先排序,或者如果您使用支持进程替换的shell(如bash,ksh或zsh),则可以动态进行排序.
join -v 1 <(sort file1) <(sort file2)
Run Code Online (Sandbox Code Playgroud)
然而,没有grep
解决方案?
仅存在于 file2 中的行:
grep -Fxvf file1 file2 > file3
Run Code Online (Sandbox Code Playgroud)仅存在于 file1 中的行:
grep -Fxvf file2 file1 > file3
Run Code Online (Sandbox Code Playgroud)两个文件中都存在的行:
grep -Fxf file1 file2 > file3
Run Code Online (Sandbox Code Playgroud)尝试
sdiff file1 file2
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,它通常对我来说效果更好.如果行的顺序不重要(例如某些文本配置文件),您可能希望先排序文件.
例如,
sdiff -w 185 file1.cfg file2.cfg
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
347703 次 |
最近记录: |