linux 中 comm命令的用法
2022/3/27 7:24:28
本文主要是介绍linux 中 comm命令的用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、测试数据
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt sss eee rrr 555 888 333 bbb 777 yyy root@ubuntu01:/home/test# cat b.txt yyy ddd sss eee 666 kkk mmm jjj
2、使用comm命令前需要对文件进行排序和去重复
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt sss eee rrr 555 888 333 bbb 777 yyy root@ubuntu01:/home/test# cat b.txt yyy ddd sss eee 666 kkk mmm jjj root@ubuntu01:/home/test# sort -u a.txt > a && mv a a.txt ## 排序去重复 root@ubuntu01:/home/test# sort -u b.txt > a && mv a b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy
3、comm命令用法:comm file1 file2
一共生成三列数据:第一列:file1中特有的、 第二列:file2中特有的、 第三列:file1和file2中共有的
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy root@ubuntu01:/home/test# comm a.txt b.txt ## 第一列a.txt中特有的; 第二列b.txt中特有的; 第三列a.txt和b.txt中共有的 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy
4、取两者交集
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy root@ubuntu01:/home/test# comm a.txt b.txt 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy root@ubuntu01:/home/test# comm a.txt b.txt -1 -2 ## 两者交集 eee sss yyy
5、取两者并集
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy root@ubuntu01:/home/test# comm a.txt b.txt 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy root@ubuntu01:/home/test# comm a.txt b.txt | sed 's/^[\t ]*//g' ## 取两者并集 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy
6、取a.txt中特有内容
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy root@ubuntu01:/home/test# comm a.txt b.txt 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy root@ubuntu01:/home/test# comm a.txt b.txt -2 -3 ## 取a.txt中特有的 333 555 777 888 bbb rrr
7、取b.txt中特有的
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy root@ubuntu01:/home/test# comm a.txt b.txt 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy root@ubuntu01:/home/test# comm a.txt b.txt -1 -3 ## 取b.txt的特有项 666 ddd jjj kkk mmm
8、取a.txt和b.txt特有项的合集
root@ubuntu01:/home/test# ls a.txt b.txt root@ubuntu01:/home/test# cat a.txt 333 555 777 888 bbb eee rrr sss yyy root@ubuntu01:/home/test# cat b.txt 666 ddd eee jjj kkk mmm sss yyy root@ubuntu01:/home/test# comm a.txt b.txt 333 555 666 777 888 bbb ddd eee jjj kkk mmm rrr sss yyy root@ubuntu01:/home/test# comm a.txt b.txt -3 | sed 's/^[\t ]*//g' ## 取两个文件特有项的合集 333 555 666 777 888 bbb ddd jjj kkk mmm rrr
这篇关于linux 中 comm命令的用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30[译]漫画SELinux概念
- 2024-03-29linux 移动文件
- 2024-03-28linux .so file
- 2024-03-28Linux 磁盘管理
- 2024-03-28Linux学习笔记(十三)磁盘管理(一):磁盘分区
- 2024-03-26linux 创建 文件
- 2024-03-25使用SecureCRT对Linux vim进行颜色设置
- 2024-03-202019-2020-12 20199317 《Linux内核原理与分析》 第十二周作业
- 2024-03-20Linux运维的第二周总结
- 2024-03-13how to count number of directories in linux