在linux操作系统下 comm命令介绍和使用案例
comm 命令是一个用于比较两个已排序文件的工具,能够逐行输出它们的差异和共同部分。它在文本处理和数据分析中非常有用,尤其是在需要找出两个文件之间的相似和不同之处时
comm 命令介绍功能比较文件:comm 命令逐行比较两个已排序的文件,并输出三列: 第一列:仅在第一个文件中存在的行。 第二列:仅在第二个文件中存在的行。 第三列:在两个文件中都存在的行。语法bashcomm [OPTION]... FILE1 FILE2常用选项-1:抑制第一列的输出(仅在第一个文件中存在的行)。-2:抑制第二列的输出(仅在第二个文件中存在的行)。-3:抑制第三列的输出(在两个文件中都存在的行)。-i:在比较时忽略大小写。-u:仅输出唯一的行。-z:使用零字节而不是换行符作为行分隔符。--check-order:检查输入是否正确排序,即使所有输入行都是可配对的。命令:
root@meng:~# which comm
/usr/bin/comm
root@meng:~# comm --help
Usage: comm [OPTION]... FILE1 FILE2
Compare sorted files FILE1 and FILE2 line by line.
When FILE1 or FILE2 (not both) is -, read standard input.
With no options, produce three-column output. Column one contains
lines unique to FILE1, column two contains lines unique to FILE2,
and column three contains lines common to both files.
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
--check-order check that the input is correctly sorted, even
if all input lines are pairable
--nocheck-order do not check that the input is correctly sorted
--output-delimiter=STR separate columns with STR
--total output a summary
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
Note, comparisons honor the rules specified by 'LC_COLLATE'.
Examples:
comm -12 file1 file2 Print only lines present in both file1 and file2.
comm -3 file1 file2 Print lines in file1 not in file2, and vice versa.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/comm>
or available locally via: info '(coreutils) comm invocation'
root@meng:~# comm
comm: missing operand
Try 'comm --help' for more information.
命令案例:
root@meng:~# cat m1.txt |sort > s1.txt
root@meng:~# cat m2.txt |sort > s2.txt
root@meng:~# cat s1.txt
This is a test
This is a test
HelloWorld
Hello World
Hello World
g
g
hello men
hello meng
root@meng:~# cat s2.txt
his is a test
HloWorld
Hlo World
Hlo World
g
g
hlo men
hlo meng
root@meng:~# comm s1.txt s2.txt
This is a test
This is his is a test
HelloWorld
Hello World
Hello World
HloWorld
Hlo World
Hlo World
g
g
hello men
hello meng
hlo men
hlo meng
root@meng:~# comm -1 s1.txt s2.txt
his is a test
HloWorld
Hlo World
Hlo World
g
g
hlo men
hlo meng
root@meng:~# comm -2 s1.txt s2.txt
This is a test
This is a test
HelloWorld
Hello World
Hello World
g
g
hello men
hello meng
root@meng:~# comm -3 s1.txt s2.txt
This is a test
This is his is a test
HelloWorld
Hello World
Hello World
HloWorld
Hlo World
Hlo World
hello men
hello meng
hlo men
hlo meng
root@meng:~# comm -12 s1.txt s2.txt
g
g
root@meng:~#