Linux comm命令
使用局限比較大,適用於特殊場合;
Linux comm命令用於比較兩個已排過序的文件。
排序:sort -u file
這項指令會一列列地比較兩個已排序文件的差異,並將其結果顯示出來;
如果沒有指定任何參數,則會把結果分成3行顯示:
- 第1行僅是在第1個文件中出現過的列
- 第2行是僅在第2個文件中出現過的列
- 第3行則是在第1與第2個文件里都出現過的列
若給予的文件名稱為"-",則comm指令會從標准輸入設備讀取數據。
語法
comm [-123][--help][--version][第1個文件][第2個文件]
參數:
- -1 不顯示只在第1個文件里出現過的列。
- -2 不顯示只在第2個文件里出現過的列。
- -3 不顯示只在第1和第2個文件里出現過的列。
- --help 在線幫助。
- --version 顯示版本信息。
實例
aaa.txt 與 bbb.txt 的文件內容如下:
[root@localhost text]# cat aaa.txt aaa bbb ccc ddd eee 111 222 [root@localhost text]# cat bbb.txt bbb ccc aaa hhh ttt jjj
執行 comm 命令輸出結果如下: [root@localhost text]# comm aaa.txt bbb.txt aaa bbb ccc aaa ddd eee 111 222 hhh ttt jjj 第一列 第二列 第三列
輸出的第一列只包含在aaa.txt中出現的行,第二列包含在bbb.txt中出現的行,第三列包含在aaa.txt和bbb.txt中相同的行。
具體實例講解:
1、以下(A==file_1 and B==file_2)
2、選項:-12 == -21 -23==-32 -31==-13
組合使用一:輸出只有一列
1、w@ubuntu:~/work/linshi$ cat aa_sort.txt
111
222
aaa
bbb
ccc
ddd
eee
2、w@ubuntu:~/work/linshi$ cat bb_sort.txt
aaa
bbb
ccc
hhh
jjj
ttt
3、comm -12 A B
表達意義:(AnB)
w@ubuntu:~/work/linshi$ comm -12 aa_sort.txt bb_sort.txt
aaa
bbb
ccc
4、comm -13 A B
表達意義:B-(AnB)
w@ubuntu:~/work/linshi$ comm -13 aa_sort.txt bb_sort.txt
hhh
jjj
ttt
5、comm -23 A B
表達意義:A-(AnB)
w@ubuntu:~/work/linshi$ comm -23 aa_sort.txt bb_sort.txt
111
222
ddd
eee
實質上:(comm -1) or (comm -2) or (comm -3),每個命令都會輸出兩列,兩列的內容 等於如下表示:
各列是以制表符(\t)作為定界符。
6、comm -1 == comm -13 -12
w@ubuntu:~/work/linshi$ comm -1 aa_sort.txt bb_sort.txt
aaa
bbb
ccc
hhh
jjj
ttt
7、comm -2 == comm -23 -21
w@ubuntu:~/work/linshi$ comm -2 aa_sort.txt bb_sort.txt
111
222
aaa
bbb
ccc
ddd
eee
8、comm -3 == comm -32 -31
w@ubuntu:~/work/linshi$ comm -3 aa_sort.txt bb_sort.txt
111
222
ddd
eee
hhh
jjj
ttt
9、默認情況下:comm A B== -23 -13 -12
w@ubuntu:~/work/linshi$ comm aa_sort.txt bb_sort.txt
111
222
aaa
bbb
ccc
ddd
eee
hhh
jjj
ttt