了解 Unix 差异命令
Understand Unix Diff Command
我正在尝试找出两个文件之间的区别,我想知道 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 命令仍会报告该文件?
在这种情况下,我们最好使用 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)
这将给出两个文件之间的公共文件
我正在尝试找出两个文件之间的区别,我想知道 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 命令仍会报告该文件?
在这种情况下,我们最好使用 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)
这将给出两个文件之间的公共文件