mac 比較兩個文件


比較兩個文件,輸出兩個文件都有的行,可以

1.使用comm命令

如下例:

------------------->$ 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命令會輸出三列,第一列為第一個文件單有的行,第二列為第二個文件單有的行,第三列為公有的行。另如果有重復行,並不算一行來區分。

看comm的用法:

------------------->$ comm -h
comm: illegal option -- h
usage: comm [-123i] file1 file2

可以選擇輸出那一列,比如輸出第三列,需要使用-12,注意,不顯示那一列,就把該列加到命令中去:

------------------->$ 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

 

如果想去重可以使用命令:

sort test.txt | uniq

 

2.使用grep命令

------------------->$ 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命令

先將兩個文件分別去重,再組合到一起,那么兩個文件都有的行 在組合文件中會有兩行,通過uniq顯示重復行可以打印公有部分。

命令參數:

-c或——count:在每列旁邊顯示該行重復出現的次數;
-d或--repeated:僅顯示重復出現的行列;
-f<欄位>或--skip-fields=<欄位>:忽略比較指定的欄位;
-s<字符位置>或--skip-chars=<字符位置>:忽略比較指定的字符;
-u或——unique:僅顯示出一次的行列;
-w<字符位置>或--check-chars=<字符位置>:指定要比較的字符。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM