Linux comm命令求出文件的交集、差集
2023-09-11 14:18:06 时间
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 1 is not in sorted order comm: file 2 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 -1 -2 gold orange
A对B的差集就是第一列,B对A的差集就是第二列。
[root@xuexi tmp]# comm set1.txt set2.txt -2 -3 # A对B的差集 apple iron silver steel
[root@xuexi tmp]# comm set1.txt set2.txt -1 -3 # B对A的差集 carrot cookiee
A和B的求差就是第一列和第二列的组合。
[root@xuexi tmp]# comm set1.txt set2.txt -3 apple carrot cookiee iron silver steel
但是这样分两列的结果不方便查看,应该进行处理使它们显示在同一列上。
[root@xuexi tmp]# comm set1.txt set2.txt -3 | tr "\t" "\0" apple carrot cookiee iron silver steel
相关文章
- Linux SCP命令远程复制文件
- 用linux mail命令发送邮件[Linux]
- Linux之查看文件内容命令 cat more gedit 管道
- Linux之复制、移动文件及目录命令
- Linux 系统中使用 logwatch 监控日志文件
- 使用od命令,linux下以ASCII方式查看文件
- Linux常用命令-文件操作 网络命令 性能命令
- linux下如何使rtc设备注册为指定的设备文件/dev/rtc1?
- 【Linux】linux中自动定时备份mysql数据
- Linux下面使用命令如何运行.sh文件的两种解决办法
- nginx验证微信文件(linux服务器添加校验文件)
- Linux_解决nohup命令生成的多余的大日志文件
- linux文件命令汇总
- Linux经常使用命令-文件搜索命令-文件搜索命令find
- 【Linux】linux经常使用基本命令
- Windows和linux通过命令互传文件
- Linux 目录和文件管理的命令
- Linux必须熟记的文件
- Linux中/etc/rc开头文件详细解释
- linux下系统启动时,几个配置文件 /etc/profile、~/.bash_profile 等几个文件的执行过程,先后顺序
- 每天一个linux命令(19):Linux 目录结构
- C#读取文件或者字符流的最后几行,类似linux的tail命令-OK
- C#读取文件或者字符流的最后几行,类似linux的tail命令-OK
- Linux搭建开源企业云盘Seafile,私有文件同步云盘及基本使用
- Linux搭建samba文件共享服务器,实现基于Linux和Windows的共享文件服务
- Linux C程序多文件编译