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 @   小鲨鱼2018  阅读(233)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-05-27 c语言 9-12
2021-05-27 c语言 9-11
2021-05-27 c语言 9-10
2021-05-27 c语言中大小写字符转换
2021-05-27 c语言 9-9
2021-05-27 c语言中统计字符串中数字出现的次数
2021-05-27 c语言 9-8
点击右上角即可分享
微信分享提示