linux:使用comm命令比较两个文件:交集、差

35 篇文章 0 订阅

comm命令可以按行比较两个排序好的文件,输出有3列:第一列是file1独有的、第二列是file2独有的,第三列是两者都有的,简单语法如下:

NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

DESCRIPTION
       Compare sorted files FILE1 and FILE2 line by line.

       With no options, produce three-column output.  Column one contains lines unique to FILE1, column two contains lines
       unique to FILE2, and column three contains lines common to both files.

       -1     suppress column 1 (lines unique to FILE1)

       -2     suppress column 2 (lines unique to FILE2)

       -3     suppress column 3 (lines that appear in both files)

       --check-order
              check that the input is correctly sorted, even if all input lines are pairable

       --nocheck-order
              do not check that the input is correctly sorted

       --output-delimiter=STR
              separate columns with STR

示例:先从 词典里按顺序随机抽取一些行导出到文件中,这样就省得排序了:
qingsong@db2a:/tmp$ sed -n '5p;1001p;3000p;4000p;5000p;7000p;8800p;9900p;10000p' /usr/share/dict/american-english > file1
qingsong@db2a:/tmp$ sed -n '2p;4000p;5000p;8888p;10000p;30000p;40000p' /usr/share/dict/american-english > file2
qingsong@db2a:/tmp$ cat file1
ABM's
Ashikaga's
Charybdis's
Decker
Eurasia
Idaho's
Lipizzaner
Meghan's
Merck's
qingsong@db2a:/tmp$ cat file2
A's
Decker
Eurasia
Lombard's
Merck's
collaborated
elms

比较两个文件

qingsong@db2a:/tmp$ comm file1 file2

qingsong@db2a:/tmp$ comm file1 file2
        A's
ABM's
Ashikaga's
Charybdis's
                Decker
                Eurasia
Idaho's
Lipizzaner
        Lombard's
Meghan's
                Merck's
        collaborated
        elms

只显示file1独有的行:

需要把第2列和第3列去掉:
qingsong@db2a:/tmp$ comm -2 -3 file1 file2
ABM's
Ashikaga's
Charybdis's
Idaho's
Lipizzaner
Meghan's

只显示file2独有的行:

qingsong@db2a:/tmp$ comm -1 -3 file1 file2
A's
Lombard's
collaborated
elms

只显示两者重复的行:

qingsong@db2a:/tmp$ comm -1 -2 file1 file2
Decker
Eurasia
Merck's

只显示两者不重复的行:

后面的sed是将以\t开头的\t去掉:
qingsong@db2a:/tmp$ comm -3 file1 file2 | sed 's/^\t//'
A's
ABM's
Ashikaga's
Charybdis's
Idaho's
Lipizzaner
Lombard's
Meghan's
collaborated
elms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值