mac 比较两个文件

比较两个文件,输出两个文件都有的行,可以

1.使用comm命令

如下例:

------------------->$ cat 1s1.txt
line 0
line 1
line 1
line 2
line 3
line 4


------------------->$ cat 1s2.txt
line 1
line 3
line 3
line 6


------------------->$ comm 1s1.txt 1s2.txt
line 0
                 line 1
line 1
line 2
                 line 3
        line 3
line 4
        line 6

 

comm命令会输出三列,第一列为第一个文件单有的行,第二列为第二个文件单有的行,第三列为公有的行。另如果有重复行,并不算一行来区分。

看comm的用法:

------------------->$ comm -h
comm: illegal option -- h
usage: comm [-123i] file1 file2

可以选择输出那一列,比如输出第三列,需要使用-12,注意,不显示那一列,就把该列加到命令中去:

------------------->$ comm -3 1s1.txt 1s2.txt
line 0
line 1
line 2
       line 3
line 4
       line 6


------------------->$ comm -12 1s1.txt 1s2.txt
line 1
line 3

 

如果想去重可以使用命令:

sort test.txt | uniq

 

2.使用grep命令

------------------->$ grep -f 1s1.txt 1s2.txt
line 1
line 3
line 3


------------------->$ grep -f 1s2.txt 1s1.txt
line 1
line 1
line 3


------------------->$ grep -f 1s1.txt 1s2.txt && grep -f 1s2.txt 1s1.txt
line 1
line 3
line 3
line 1
line 1
line 3

 

3.uniq命令

先将两个文件分别去重,再组合到一起,那么两个文件都有的行 在组合文件中会有两行,通过uniq显示重复行可以打印公有部分。

命令参数:

-c或——count:在每列旁边显示该行重复出现的次数;
-d或--repeated:仅显示重复出现的行列;
-f<栏位>或--skip-fields=<栏位>:忽略比较指定的栏位;
-s<字符位置>或--skip-chars=<字符位置>:忽略比较指定的字符;
-u或——unique:仅显示出一次的行列;
-w<字符位置>或--check-chars=<字符位置>:指定要比较的字符。

 

转载于:https://www.cnblogs.com/drizzlewithwind/p/9535774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值