歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux命令 >> Linux comm命令參數及使用方法詳解

Linux comm命令參數及使用方法詳解

日期:2017/2/27 16:38:10   编辑:Linux命令
comm(common)
功能說明:比較兩個已排過序的文件。
comm命令可用於兩個文件直接的比較。它有很多不錯的選項可用來調整輸出,以便我們執行交集、求差(difference)以及差集操作
交集:打印出兩個文件所共有的行
求差:打印出知道文件所包含的且不相同的那些行
差集:打印出包含在文件A中,但不包含在其他指定文件中的那些行

需要注意的是comm必須使用排過序的文件作為輸入

語  法:comm [-123][--help][--version][第1個文件][第2個文件]

補充說明:這項指令會一列列地比較兩個已排序文件的差異,並將其結果顯示出來,如果沒有指定任何參數,則會把結果分成3行顯示:第1行僅是在第1個文件中出現過的列,第2行是僅在第2個文件中出現過的列,第3行則是在第1與第2個文件裡都出現過的列。若給予的文件名稱為"-",則comm指令會從標准輸入設備讀取數據。

參  數:
-1 不顯示只在第1個文件裡出現過的列。
-2 不顯示只在第2個文件裡出現過的列。
-3 不顯示只在第1和第2個文件裡出現過的列。
--help 在線幫助。
--version 顯示版本信息。

示例:
[root@server1 linuxeye]# cat A.txt
apple
orange
gold
silver
steel
iron
[root@server1 linuxeye]# cat B.txt
orange
gold
cookies
carrot
[root@server1 linuxeye]# sort A.txt -o A.txt
[root@server1 linuxeye]# sort B.txt -o B.txt
[root@server1 linuxeye]# comm A.txt B.txt
apple
carrot
cookies
gold
iron
orange
silver
steel
輸出的第一列包含只在A.txt中出現的行,第二列包含只在B.txt中出現的行,第三列包含A.txt和B.txt中都出現的行

[root@server1 linuxeye]# comm A.txt B.txt -1 -2 #打印兩個文件的交集,即刪除第一列和第二列
orange
gold
[root@server1 linuxeye]# comm A.txt B.txt -3 #打印兩個文件中不相同的行,即刪除第三列
apple
carrot
cookies
iron
silver
steel

[root@server1 linuxeye]# comm A.txt B.txt -3 | sed 's/^\t//'
apple
carrot
cookies
iron
silver
steel

[root@server1 linuxeye]# comm A.txt B.txt -23 #A.txt的差集
apple
iron
silver
steel
[root@server1 linuxeye]# comm A.txt B.txt -13 #B.txt的差集
carrot
cookies
Copyright © Linux教程網 All Rights Reserved