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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!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