linux 中 comm命令的用法


1、測試數據

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
sss
eee
rrr
555
888
333
bbb
777
yyy
root@ubuntu01:/home/test# cat b.txt
yyy
ddd
sss
eee
666
kkk
mmm
jjj

 

2、使用comm命令前需要對文件進行排序和去重復

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
sss
eee
rrr
555
888
333
bbb
777
yyy
root@ubuntu01:/home/test# cat b.txt
yyy
ddd
sss
eee
666
kkk
mmm
jjj
root@ubuntu01:/home/test# sort -u a.txt > a && mv a a.txt   ## 排序去重復
root@ubuntu01:/home/test# sort -u b.txt > a && mv a b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy

 

3、comm命令用法:comm file1 file2

一共生成三列數據:第一列:file1中特有的、 第二列:file2中特有的、  第三列:file1和file2中共有的

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy
root@ubuntu01:/home/test# comm a.txt b.txt ## 第一列a.txt中特有的; 第二列b.txt中特有的; 第三列a.txt和b.txt中共有的 333
555
        666
777
888
bbb
        ddd
                eee
        jjj
        kkk
        mmm
rrr
                sss
                yyy

 

 

4、取兩者交集

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy
root@ubuntu01:/home/test# comm a.txt b.txt
333
555
        666
777
888
bbb
        ddd
                eee
        jjj
        kkk
        mmm
rrr
                sss
                yyy
root@ubuntu01:/home/test# comm a.txt b.txt -1 -2 ## 兩者交集
eee
sss
yyy

 

5、取兩者並集

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy
root@ubuntu01:/home/test# comm a.txt b.txt
333
555
        666
777
888
bbb
        ddd
                eee
        jjj
        kkk
        mmm
rrr
                sss
                yyy
root@ubuntu01:/home/test# comm a.txt b.txt | sed 's/^[\t ]*//g' ## 取兩者並集
333
555
666
777
888
bbb
ddd
eee
jjj
kkk
mmm
rrr
sss
yyy

 

6、取a.txt中特有內容

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy
root@ubuntu01:/home/test# comm a.txt b.txt
333
555
        666
777
888
bbb
        ddd
                eee
        jjj
        kkk
        mmm
rrr
                sss
                yyy
root@ubuntu01:/home/test# comm a.txt b.txt -2 -3 ## 取a.txt中特有的
333
555
777
888
bbb
rrr

 

7、取b.txt中特有的

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy
root@ubuntu01:/home/test# comm a.txt b.txt
333
555
        666
777
888
bbb
        ddd
                eee
        jjj
        kkk
        mmm
rrr
                sss
                yyy
root@ubuntu01:/home/test# comm a.txt b.txt -1 -3 ## 取b.txt的特有項
666
ddd
jjj
kkk
mmm

 

8、取a.txt和b.txt特有項的合集

root@ubuntu01:/home/test# ls
a.txt  b.txt
root@ubuntu01:/home/test# cat a.txt
333
555
777
888
bbb
eee
rrr
sss
yyy
root@ubuntu01:/home/test# cat b.txt
666
ddd
eee
jjj
kkk
mmm
sss
yyy
root@ubuntu01:/home/test# comm a.txt b.txt
333
555
        666
777
888
bbb
        ddd
                eee
        jjj
        kkk
        mmm
rrr
                sss
                yyy
root@ubuntu01:/home/test# comm a.txt b.txt -3 | sed 's/^[\t ]*//g' ## 取兩個文件特有項的合集
333
555
666
777
888
bbb
ddd
jjj
kkk
mmm
rrr

 


免責聲明!

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



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