linux 差集,Linux comm命令求出文件的交集、差集

A(1,2,3)和B(3,4,5),A和B的交集是3,A对B的差集是1和2,B对A的差集是4和5,A和B求差的结果是1、2、4、5。

在Linux中可以使用comm命令求出这些集。

[root@linuxidc tmp]# cat <set1.txt>orange>gold>apple>sliver>steel>iron> eof

[root@linuxidc tmp]# cat <set2.txt>orange>gold>cookiee>carrot> eof

使用comm命令。

[root@linuxidc tmp]# comm set1.txt set2.txt

apple

orange

comm:file 1 is not insorted order

comm:file 2 is not insorted order

gold

cookiee

carrot

silver

steel

iron

提示没有排序,所以comm必须要保证比较的文件是有序的。

[root@linuxidc tmp]# sort set1.txt -o set1.txt;sort set2.txt -o set2.txt

[root@linuxidc tmp]# comm set1.txt set2.txt

apple

carrot

cookiee

gold

iron

orange

silver

steel

结果中输出了3列,每一列使用制表符\t隔开。第一列是set1.txt中有而set2.txt中没有的,第二列则是set2.txt中有而set1.txt中没有的,第三列是set1.txt和set2.txt中都有的。

根据这三列就可以求出交集、差集和求差。

交集就是第三列。使用-1和-2分别删除第一第二列就是第三列的结果。

[root@linuxidc tmp]# comm set1.txt set2.txt -1 -2gold

orange

A对B的差集就是第一列,B对A的差集就是第二列。

[root@linuxidc tmp]# comm set1.txt set2.txt -2 -3# A对B的差集

apple

iron

silver

steel

[root@linuxidc tmp]# comm set1.txt set2.txt -1 -3# B对A的差集

carrot

cookiee

A和B的求差就是第一列和第二列的组合。

[root@linuxidc tmp]# comm set1.txt set2.txt -3apple

carrot

cookiee

iron

silver

steel

但是这样分两列的结果不方便查看,应该进行处理使它们显示在同一列上。

[root@linuxidc tmp]# comm set1.txt set2.txt -3 | tr "\t" "\0"apple

carrot

cookiee

iron

silver

steel

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值