shell文件对比

1、comm命令

COMM(1) BSD General Commands Manual COMM(1)

NAME
comm – select or reject lines common to two files

SYNOPSIS
comm [-123i] file1 file2

DESCRIPTION
The comm utility reads file1 and file2, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files.

 The filename ``-'' means the standard input.

 The following options are available:

 -1      Suppress printing of column 1.

 -2      Suppress printing of column 2.

 -3      Suppress printing of column 3.
  Each column will have a number of tab characters prepended to it equal to the number of lower numbered columns that are being printed.  For example, if column number two is being suppressed, lines printed in column number one will not have any tabs preceding them, and lines printed in column number three will have one.
 The comm utility assumes that the files are lexically sorted; all characters participate in line comparisons.

中文简介就是comm [-123i] file1 file2,123分别是显示(抑制)文件1独有、文件2独有、文件1和2共有;i表示忽略大小写。对比时按行对比。
使用comm命令时,文件应该是排序了的。下面简单举例:
假设文件1和2的内容分别是:

file1:                   file2:
a                       c
b                       d
c                       a
e                       c
d
a

直接运行:

comm fiel1 file2
a
b
            c
    d
    a
    c
e
d
a

排序结果分析:先对比file1的”a”和file2的”c”,按字典顺序,输出”a”,接着比较file1的”b”和file2的”c”,输出”b”,然后会同时输出”c”,再比较file1的”e”和file2的”d”,输出”d”….直至最后。
排序后输出:

comm <(sort file1 ) <(sort file2 )
             a
a
b
            c
    c
            d
e

结果不解释,类似上面

排序且唯一:

comm <(sort file1|uniq ) <(sort file2|uniq )
            a
b
            c
            d
e

只求交集

comm -12 <(sort file1|uniq ) <(sort file2|uniq )  
            a
            c
            d
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值