比較兩個文件,輸出兩個文件都有的行,能夠spa
1.使用comm命令code
以下例:blog
------------------->$ cat 1s1.txt line 0 line 1 line 1 line 2 line 3 line 4 ------------------->$ cat 1s2.txt line 1 line 3 line 3 line 6 ------------------->$ comm 1s1.txt 1s2.txt line 0 line 1 line 1 line 2 line 3 line 3 line 4 line 6
comm命令會輸出三列,第一列爲第一個文件單有的行,第二列爲第二個文件單有的行,第三列爲公有的行。另若是有重複行,並不算一行來區分。ip
看comm的用法:io
------------------->$ comm -h comm: illegal option -- h usage: comm [-123i] file1 file2
能夠選擇輸出那一列,好比輸出第三列,須要使用-12,注意,不顯示那一列,就把該列加到命令中去:class
------------------->$ comm -3 1s1.txt 1s2.txt line 0 line 1 line 2 line 3 line 4 line 6 ------------------->$ comm -12 1s1.txt 1s2.txt line 1 line 3
若是想去重可使用命令:test
sort test.txt | uniq
2.使用grep命令file
------------------->$ grep -f 1s1.txt 1s2.txt line 1 line 3 line 3 ------------------->$ grep -f 1s2.txt 1s1.txt line 1 line 1 line 3 ------------------->$ grep -f 1s1.txt 1s2.txt && grep -f 1s2.txt 1s1.txt line 1 line 3 line 3 line 1 line 1 line 3
3.uniq命令grep
先將兩個文件分別去重,再組合到一塊兒,那麼兩個文件都有的行 在組合文件中會有兩行,經過uniq顯示重複行能夠打印公有部分。sort
命令參數:
-c或——count:在每列旁邊顯示該行重複出現的次數; -d或--repeated:僅顯示重複出現的行列; -f<欄位>或--skip-fields=<欄位>:忽略比較指定的欄位; -s<字符位置>或--skip-chars=<字符位置>:忽略比較指定的字符; -u或——unique:僅顯示出一次的行列; -w<字符位置>或--check-chars=<字符位置>:指定要比較的字符。