Linux diff命令
时间:2019-11-20 08:52:49 来源:igfitidea点击:
使用Linux diff命令时,如何显示文件共有的行?
解决方案
要显示两个文件共有的行,可以使用comm命令替代。
它将逐行比较两个排序的文件。如果没有选项,则产生三列输出。第一列包含FILE1独有的行,第二列包含FILE2独有的行,第三列包含两个文件共同的行。
显示文件1和文件2共有的行
执行命令,如下所示:
$ comm /path/to/file1/ /path/to/file2 $ comm -1 /path/to/file1/ /path/to/file2 $ comm -2 /path/to/file1/ /path/to/file2 $ comm -3 /path/to/file1/ /path/to/file2
其中
-1:不显示FILE1特有的行-2:不显示FILE2独有的行-3:不显示在两个文件中都有的行
或者我们使用perl脚本实现:
$ perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' file1 file2
file1
linux hello theitroad world
file2
hello java world theitroad on it road
[Hyman@theitroadcentos7 tmp]# comm -1 file1 file2
hello
java
comm: file 1 is not in sorted order
world
comm: file 2 is not in sorted order
theitroad
on it road
[Hyman@theitroadcentos7 tmp]# comm -2 file1 file2
linux
comm: file 1 is not in sorted order
hello
theitroad
world
comm: file 2 is not in sorted order
[Hyman@theitroadcentos7 tmp]# comm -3 file1 file2
hello
java
linux
comm: file 1 is not in sorted order
hello
theitroad
comm: file 2 is not in sorted order
theitroad
on it road
[Hyman@theitroadcentos7 tmp]#

