了解Unix Diff命令

时间:2018-06-14 11:02:35

标签: shell unix diff

我试图找到两个文件之间的区别,在哪里我想知道file_2中的新条目。例如:

如果a.txt包含:

a
b
c

b.txt包含:

c
d
f

我想获得d and f

我使用命令:diff --changed-group-format="%>" --unchanged-group-format=''

mymach@dev-machine:~/test$ grep 'C:/Documents and Settings/pandep2/AppData/Local/Google/Chrome/User Data/CrashpadMetrics.pma~RF115cef5.TMP' file_1.log
C:/Documents and Settings/pandep2/AppData/Local/Google/Chrome/User Data/CrashpadMetrics.pma~RF115cef5.TMP

mymach@dev-machine:~/test$ grep 'C:/Documents and Settings/pandep2/AppData/Local/Google/Chrome/User Data/CrashpadMetrics.pma~RF115cef5.TMP' file_2.log
C:/Documents and Settings/pandep2/AppData/Local/Google/Chrome/User Data/CrashpadMetrics.pma~RF115cef5.TMP

mymach@dev-machine:~/test$ diff --changed-group-format="%>" --unchanged-group-format='' file_1.log file_2.log >diff_file.log

mymach@dev-machine:~/test$ grep 'C:/Documents and Settings/pandep2/AppData/Local/Google/Chrome/User Data/CrashpadMetrics.pma~RF115cef5.TMP' diff_file.log
C:/Documents and Settings/pandep2/AppData/Local/Google/Chrome/User Data/CrashpadMetrics.pma~RF115cef5.TMP

因为,两个文件中都存在相同的文件,为什么diff命令仍会报告该文件?

1 个答案:

答案 0 :(得分:1)

在这种情况下,我们最好在Unix中使用comm命令。

对于上述场景,我使用过:

comm -23 <(sort file_1.txt) <(sort file_2.txt)

这将提供file_1.txt的唯一文件

comm -13 <(sort a.txt) <(sort b.txt)

这将提供file_2.txt的唯一文件

comm -12 <(sort a.txt) <(sort b.txt)

这将提供两个文件之间的公共文件