求两个Linux文本文件的交集、差集、并集
2017-06-08 10:34
260 查看
一、交集
sort a.txt b.txt | uniq -d二、并集
sort a.txt b.txt | uniq三、差集
a.txt-b.txt:sort a.txt b.txt b.txt | uniq -u
b.txt - a.txt:
sort b.txt a.txt a.txt | uniq -u
四、相关的解释
使用sort可以将文件进行排序(sort排序是为了管道交给uniq进行处理,uniq只能处理相邻的行),可以使用sort后面的参数,例如 -n 按照数字格式排序,例如 -i 忽略大小写,例如使用-r 为逆序输出等uniq为删除文件中重复的行,得到文件中唯一的行,参数-d 表示的是输出出现次数大于1的内容;参数-u表示的是输出出现次数为1的内容;那么对于上述的求交集并集差集的命令做如下的解释:
sort a.txt b.txt | uniq -d:将两个文件进行排序,uniq使得两个文件中的内容为唯一的,使用-d输出两个文件中次数大于1的内容,即是得到交集
sort a.txt b.txt | uniq :将两个文件进行排序,uniq使得两个文件中的内容为唯一的,即可得到两个文件的并集
sort a.txt b.txt b.txt | uniq -u:将两个文件排序,最后输出a.txt b.txt b.txt文件中只出现过一次的内容,因为有两个b.txt所以只会输出只在a.txt出现过一次的内容(b.txt的内容至少出现两次),即是a.txt-b.txt差集;对于b.txt-a.txt同理。
样例
# a.hosts[root(0)@thatsit 11:40:46 ~/scripts]# cat a.hosts 10.10.1.101 10.10.1.102 10.10.1.103 10.10.1.104 [root(0)@thatsit 11:40:47 ~/scripts]#
# b.hosts
[root(0)@thatsit 11:40:48 ~/scripts]# cat b.hosts 10.10.1.101 10.10.1.103 10.10.1.105 [root(0)@thatsit 11:40:49 ~/scripts]#
# a.hosts ∩ b.hosts
[root(0)@thatsit 11:40:49 ~/scripts]# sort a.hosts b.hosts | uniq -d 10.10.1.101 10.10.1.103 [root(0)@thatsit 11:41:08 ~/scripts]#
# a.hosts ∪ b.hosts
[root(0)@thatsit 11:41:10 ~/scripts]# sort a.hosts b.hosts | uniq 10.10.1.101 10.10.1.102 10.10.1.103 10.10.1.104 10.10.1.105 [root(0)@thatsit 11:41:19 ~/scripts]#
# a.hosts - b.hosts
[root(0)@thatsit 11:41:25 ~/scripts]# sort a.hosts b.hosts b.hosts | uniq -u 10.10.1.102 10.10.1.104 [root(0)@thatsit 11:41:45 ~/scripts]#
# b.hosts - a.hosts
[root(0)@thatsit 11:41:47 ~/scripts]# sort a.hosts a.hosts b.hosts | uniq -u 10.10.1.105 [root(0)@thatsit 11:41:55 ~/scripts]#
参考链接:http://www.cnblogs.com/molong1208/p/5358509.html
相关文章推荐
- Python求两个文本文件以行为单位的交集、并集与差集的方法
- Linux 两个文件求交集、并集、差集
- Linux 两个文件求交集、并集、差集
- Python求两个文本文件以行为单位的交集、并集与差集的方法
- linux命令求两个文件的交集、差集、并集等
- linux求两个文件的交集,并集,差集
- Python 求两个文本文件以行为单位的交集 并集 差集
- python获得两个数组交集、并集、差集的方法
- js取两个数组的交集|差集|并集|补集|去重示例代码
- linux中2个文件的并集,交集,差集
- Linux文件比较操作---文本文件的交集、差集与求差
- python两个 list 获取交集,并集,差集的方法.
- 两个DataTable之间的差集、交集、并集集合(Except、Intersect、Union)
- linux_coom _ Linux文件比较,文本文件的交集、差集与求差
- 用C++标准库算法求两个集合的交集、并集、差集
- 输入两个闭区间,求其交集,并集和差集(C++):
- python取两个list的交集并集差集
- python 求两个list的差集,并集和交集
- linux 文本文件的交集,差集 comm命令
- 求两个数组的交集、并集和差集算法分析与实现