zoukankan      html  css  js  c++  java
  • Linux 对比两个文本的交集和差集(comm)

    介绍

    comm命令可以对两个已排序好的文本的内容进行交集和差集的对比,记住必须是已排序过的文件;可以使用sort命令对没有排序的文件进行排序,comm命令在对比结果中会产生三列分别是:在A中不在B中的内容,在B中不在A中的内容,AB的交集的内容。

    事例

    [root@localhost test]# cat a
    3 c
    2 b
    1 a
    
    [root@localhost test]# cat b
    2 b
    3 c
    4 d

    其中文件a不是倒序的文件,看看直接拿来对比会出现什么问题。

    [root@localhost test]# comm a b
        2 b
            3 c
    comm: file 1 is not in sorted order
    2 b
    1 a
    
        4 d

    对比结果出现了问题提示文件1不是已排序的文件。

    1.对文件进行排序

    [root@localhost test]# sort a -o a
    [root@localhost test]# cat a
    1 a
    2 b
    3 c

    2.对比文件

    [root@localhost test]# comm a b
    1 a
            2 b
            3 c
        4 d

    第一列:在a文件中不在b文件中的内容

    第二列:在b文件中不在a文件中的内容

    第三列:a文件和b文件的交集

    comm命令参数

    -1:不显示第一列

    -2:不显示第二列

    -3:不显示第三列

    [root@localhost test]# comm a b -1
        2 b
        3 c
    4 d
    [root@localhost test]# comm a b -2
    1 a
        2 b
        3 c
    [root@localhost test]# comm a b -3
    1 a
        4 d
    [root@localhost test]# comm a b -12
    2 b
    3 c

    其它的一些特殊处理方法

    [root@localhost test]# comm a b -3
    1 a
        4 d
    [root@localhost test]# comm a b -3 | sed 's/^	//'
    1 a
    4 d

    可以使用sed命令将开头的制表符(tab)替换掉,s:替换的意思,^:以什么开头, :制表符,//:空

    总结

    备注:

        作者:pursuer.chen

        博客:http://www.cnblogs.com/chenmh

    本站点所有随笔都是原创,欢迎大家转载;但转载时必须注明文章来源,且在文章开头明显处给明链接。

    《欢迎交流讨论》

  • 相关阅读:
    【iOS】获取App的常用文件路径
    【iOS】如何在Objective-C中声明Block?
    实用终端小命令
    iOS开发之NSBundle加载自定义cell需指定其的identifier
    【转】nonatomic, retain,weak,strong用法详解
    iOS控制台打印NSLog增强版
    iOS 内存中的ViewController释放
    iOS ViewController生命周期
    Netty (一) IO 基础篇
    多线程(七) 线程池的实现原理分析
  • 原文地址:https://www.cnblogs.com/chenmh/p/5531149.html
Copyright © 2011-2022 走看看