加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

LINUX实操:Linux comm命令求出文件的交集、差集

发布时间:2020-12-13 17:46:23 所属栏目:Linux 来源:网络整理
导读:《LINUX实操:Linux comm命令求出文件的交集、差集》要点: 本文介绍了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

《LINUX实操:Linux comm命令求出文件的交集、差集》要点:
本文介绍了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 <<eof>set1.txt
> orange
> gold
> apple
> sliver
> steel
> iron
> eof
?
[root@linuxidc tmp]# cat <<eof>set2.txt
> orange
> gold
> cookiee
> carrot
> eof

使用comm敕令.

[root@linuxidc tmp]# comm set1.txt set2.txt
apple
                orange
comm: file 1 is not in sorted order
comm: file 2 is not in sorted 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 -2
gold
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  -3  
apple
        carrot
        cookiee
iron
silver
steel

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

[root@linuxidc tmp]# comm set1.txt set2.txt  -3 | tr "t" ""
apple
carrot
cookiee
iron
silver
steel

本文永远更新链接地址

《LINUX实操:Linux comm命令求出文件的交集、差集》是否对您有启发,欢迎查看更多与《LINUX实操:Linux comm命令求出文件的交集、差集》相关教程,学精学透。编程之家PHP学院为您提供精彩教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读