Linux comm命令求出文件的交集、差集
2024-08-28 05:21:32
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@xuexi tmp]# cat <<eof>set1.txt
> orange
> gold
> apple
> sliver
> steel
> iron
> eof
[root@xuexi tmp]# cat <<eof>set2.txt
> orange
> gold
> cookiee
> carrot
> eof
使用comm命令。
[root@xuexi tmp]# comm set1.txt set2.txt
apple
orange
comm: file is not in sorted order
comm: file is not in sorted order
gold
cookiee
carrot
silver
steel
iron
提示没有排序,所以comm必须要保证比较的文件是有序的。
[root@xuexi tmp]# sort set1.txt -o set1.txt;sort set2.txt -o set2.txt
[root@xuexi 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@xuexi tmp]# comm set1.txt set2.txt - -
gold
orange
A对B的差集就是第一列,B对A的差集就是第二列。
[root@xuexi tmp]# comm set1.txt set2.txt - - # A对B的差集
apple
iron
silver
steel
[root@xuexi tmp]# comm set1.txt set2.txt - - # B对A的差集
carrot
cookiee
A和B的求差就是第一列和第二列的组合。
[root@xuexi tmp]# comm set1.txt set2.txt -
apple
carrot
cookiee
iron
silver
steel
但是这样分两列的结果不方便查看,应该进行处理使它们显示在同一列上。
[root@xuexi tmp]# comm set1.txt set2.txt - | tr "\t" "\0"
apple
carrot
cookiee
iron
silver
steel
最新文章
- Install wget for mac
- C++之内联函数与constexpr
- BZOJ1036 树的统计
- Windows 的 AD 域寄生于 Linux 机器
- RFIDler:一款定义RFID的读、写、仿真器的开源软件
- (转)ASP.NET缓存全解析6:数据库缓存依赖
- acm-DP整理
- Spring顶级项目以及Spring cloud组件
- BOM和DOM的联系和区别
- Mysql 嵌套游标添以及任意位置声明变量的方法
- angularjs bind与model配合双向绑定 表达式方法输出
- 关于strcpy函数形参类型的解析和指针作为输入型输出型参数的不同
- bootstrap table使用参考
- ESP8266使用详解(AT,LUA,SDK)
- scala recursive value x$5 needs type
- springboot mybatis pagehelper 分页问题
- 山东省第四届ACM程序设计竞赛部分题解
- 《转》深入理解Activity启动流程(四)–Activity Task的调度算法
- javascript 面向对象 new 关键字 原型链 构造函数
- 【转】Context.getExternalFilesDir()和Context.getExternalCacheDir()方法
热门文章
- win10更新永久关闭
- vuejs 使用vue-cli引入bootstrap
- navicat连接mysql出现2059错误
- recyclerview刷新
- STM32-跑马灯实验
- Windows下安装BeautifulSoup4显示&#39;You are trying to run the Python 2 version of Beautiful Soup under Python 3.(`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).&#39;
- PyCharm远程开发和调试
- 为什么我们要使用int类型来保存时间类型的数据。
- spring-data-redis和jedis版本对应收集总结
- 一套高可用、易伸缩、高并发的IM群聊架构方案设计实践