使用cat,sort和uniq命令做集合操作

比如你有两个文件列表,想比较出哪些文件是共有的,哪些是独有的怎么办?

原理:

前提是两个列表文件中各自的内容不能出现重复(否则可以sort -u处理下);cat命令将两个文件混合, sort 使它有序,然后利用uniq命令的-d参数(只打印有重复的内容)和-u参数(只打印不重复的内容)

示例:

假设list1.txt内容为

a.mp4
b.mp4

list2.txt内容为

b.mp4
c.mp4

并集

cat list1.txt list2.txt
输出
a.mp4
b.mp4
c.mp4

交集

cat list1.txt list2.txt | sort | uniq -d
输出
b.mp4

list1.txt独有的

cat list1.txt list2.txt | sort | uniq -d > both
both的内容为
b.mp4

cat list1.txt both | sort | uniq -u
输出
a.mp4

list2.txt独有的

cat list1.txt list2.txt | sort | uniq -d > both
both的内容为
b.mp4

cat list2.txt both | sort | uniq -u
输出
c.mp4

 

转载于:https://my.oschina.net/tinyhare/blog/3102295

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值