比如你有两个文件列表,想比较出哪些文件是共有的,哪些是独有的怎么办?
原理:
前提是两个列表文件中各自的内容不能出现重复(否则可以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