linux 中统计两个文件的交集、并集、特有项

 

分类1:当两个文件没有重复项时。

1、测试数据

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt       ## 测试数据
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
e
f
g

 

2、交集

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
e
f
g
root@PC1:/home/test4# cat a.txt b.txt | sort | uniq -d
c
d
e
root@PC1:/home/test4# cat a.txt b.txt | sort | uniq -D
c
c
d
d
e
e

 

3、并集

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
e
f
g
root@PC1:/home/test4# sort -u a.txt b.txt             ## 并集
a
b
c
d
e
f
g
root@PC1:/home/test4# cat a.txt b.txt | sort -u        ## 并集
a
b
c
d
e
f
g
root@PC1:/home/test4# cat a.txt b.txt | sort | uniq     ## 并集
a
b
c
d
e
f
g

 

4、a.txt中特有项

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
e
f
g
root@PC1:/home/test4# cat a.txt b.txt b.txt | sort | uniq -u      ## a.txt中特有项
a
b

 

5、b.txt中特有项

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
b
c
d
e
root@PC1:/home/test4# cat b.txt
c
d
e
f
g
root@PC1:/home/test4# cat a.txt a.txt b.txt | sort | uniq -u     ## b.txt中特有项
f
g

 

分类2 当两个文件中有重复项时。

1、交集

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# cat a.txt b.txt | uniq -d   ## 当两个文件中有重复项时,不能用该方法取交集
a
b
e
c
e
f

 

正确方法,先去重复,再去交集:

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# sort -u a.txt | cat - <(sort -u b.txt ) | sort | uniq -d    ## 取交集
c
d
e
root@PC1:/home/test4# sort -u a.txt | cat - <(sort -u b.txt ) | sort | uniq -D   ## 取交集
c
c
d
d
e
e

 

2、取并集

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# sort -u a.txt b.txt                ## 取并集
a
b
c
d
e
f
g
root@PC1:/home/test4# cat a.txt b.txt | sort | uniq      ## 取并集
a
b
c
d
e
f
g
root@PC1:/home/test4# cat a.txt b.txt | sort -u          ## 取并集
a
b
c
d
e
f
g

 

3、取a.txt中特有项

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# cat a.txt b.txt b.txt | sort | uniq -u      ## a.txt中有重复项时不能用该方法

 

正确做法先去重复:

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# sort -u a.txt | cat - b.txt b.txt | sort | uniq -u     ## 取a.txt中特有项
a
b

 

4、取b.txt中特有项

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# cat a.txt a.txt b.txt | sort | uniq -u  ## 当b.txt中有重复项时不适合该方法
g

 

正确做法是先对b.txt去重复:

root@PC1:/home/test4# ls
a.txt  b.txt
root@PC1:/home/test4# cat a.txt
a
a
a
b
b
c
d
e
e
root@PC1:/home/test4# cat b.txt
c
c
d
e
e
f
f
g
root@PC1:/home/test4# cat a.txt a.txt <(sort -u b.txt ) | sort | uniq -u    ## 取b.txt中的特有项
f
g

 

posted @ 2022-05-27 11:03  小鲨鱼2018  阅读(238)  评论(0)    收藏  举报