LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > Linux命令 >

Linux comm命令参数及使用方法详解

时间:2012-10-08 17:59来源:www.linuxeye.com 编辑:LinuxEye 点击:
comm(common) 功能说明: 比较两个已排过序的文件。 comm命令可用于两个文件直接的比较。它有很多不错的选项可用来调整输出,以便我们执行交集、求差(difference)以及差集操作 交集:打
comm(common)
功能说明:比较两个已排过序的文件。
comm命令可用于两个文件直接的比较。它有很多不错的选项可用来调整输出,以便我们执行交集、求差(difference)以及差集操作
交集:打印出两个文件所共有的行
求差:打印出知道文件所包含的且不相同的那些行
差集:打印出包含在文件A中,但不包含在其他指定文件中的那些行

需要注意的是comm必须使用排过序的文件作为输入

语  法:comm [-123][--help][--version][第1个文件][第2个文件]

补充说明:这项指令会一列列地比较两个已排序文件的差异,并将其结果显示出来,如果没有指定任何参数,则会把结果分成3行显示:第1行仅是在第1个文件中出现过的列,第2行是仅在第2个文件中出现过的列,第3行则是在第1与第2个文件里都出现过的列。若给予的文件名称为"-",则comm指令会从标准输入设备读取数据。

参  数:
  -1   不显示只在第1个文件里出现过的列。
  -2   不显示只在第2个文件里出现过的列。
  -3   不显示只在第1和第2个文件里出现过的列。
  --help   在线帮助。
  --version   显示版本信息。

示例:
[root@server1 linuxeye]# cat A.txt
apple
orange
gold
silver
steel
iron
[root@server1 linuxeye]# cat B.txt
orange
gold
cookies
carrot
[root@server1 linuxeye]# sort A.txt -o A.txt
[root@server1 linuxeye]# sort B.txt -o B.txt
[root@server1 linuxeye]# comm A.txt B.txt
apple
        carrot
        cookies
                gold
iron
                orange
silver
steel
输出的第一列包含只在A.txt中出现的行,第二列包含只在B.txt中出现的行,第三列包含A.txt和B.txt中都出现的行

[root@server1 linuxeye]# comm A.txt B.txt -1 -2  #打印两个文件的交集,即删除第一列和第二列
orange
gold
[root@server1 linuxeye]# comm A.txt B.txt -3     #打印两个文件中不相同的行,即删除第三列
apple
        carrot
        cookies
iron
silver
steel

[root@server1 linuxeye]# comm A.txt B.txt -3 | sed 's/^\t//'
apple
carrot
cookies
iron
silver
steel

[root@server1 linuxeye]# comm A.txt B.txt -23     #A.txt的差集
apple
iron
silver
steel
[root@server1 linuxeye]# comm A.txt B.txt -13     #B.txt的差集
carrot
cookies

转载请保留固定链接: https://linuxeye.com/command/comm.html

------分隔线----------------------------
标签:comm
栏目列表
推荐内容