linux系統中comm命令的用法


linux中comm命令用於提取兩列數據中的唯一項、交集、並集等。

1、測試數據

root@PC1:/home/test/test# ls
a.txt  b.txt
root@PC1:/home/test/test# cat a.txt
w
s
g
d
w
a
root@PC1:/home/test/test# cat b.txt
d
a
e
d
t
c

 

2、對數據去重復

root@PC1:/home/test/test# cat a.txt
w
s
g
d
w
a
root@PC1:/home/test/test# sort -u a.txt > a && mv a a.txt
root@PC1:/home/test/test# cat a.txt
a
d
g
s
w
root@PC1:/home/test/test# cat b.txt
d
a
e
d
t
c
root@PC1:/home/test/test# sort -u b.txt > a && mv a b.txt
root@PC1:/home/test/test# cat b.txt
a
c
d
e
t

 

3、測試使用

root@PC1:/home/test/test# cat a.txt
a
d
g
s
w
root@PC1:/home/test/test# cat b.txt
a
c
d
e
t
root@PC1:/home/test/test# comm a.txt b.txt
                a
        c
                d
        e
g
s
        t
w

第一列是a.txt中唯一的項, 第二列是b.txt中唯一的項, 第三列是a.txt和b.txt中共有的項

 

4、只保留a.txt中唯一的項、 或者b.txt中唯一的項、或者兩者交集

root@PC1:/home/test/test# cat a.txt
a
d
g
s
w
root@PC1:/home/test/test# cat b.txt
a
c
d
e
t
root@PC1:/home/test/test# comm a.txt b.txt
                a
        c
                d
        e
g
s
        t
w
root@PC1:/home/test/test# comm a.txt b.txt -2 -3
g
s
w
root@PC1:/home/test/test# comm a.txt b.txt -1 -3
c
e
t
root@PC1:/home/test/test# comm a.txt b.txt -1 -2
a
d

 

5、取兩者並集

root@PC1:/home/test/test# cat a.txt
a
d
g
s
w
root@PC1:/home/test/test# cat b.txt
a
c
d
e
t
root@PC1:/home/test/test# comm a.txt b.txt
                a
        c
                d
        e
g
s
        t
w
root@PC1:/home/test/test# comm a.txt b.txt  | sed 's/^[\t]\+//g'
a
c
d
e
g
s
t
w

 


免責聲明!

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



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