• linux系统中comm命令的用法


    linux中comm命令用于提取两列数据中的唯一项、交集、并集等。

    1、测试数据

    root@PC1:/home/test/test# ls
    a.txt  b.txt
    root@PC1:/home/test/test# cat a.txt
    w
    s
    g
    d
    w
    a
    root@PC1:/home/test/test# cat b.txt
    d
    a
    e
    d
    t
    c

    2、对数据去重复

    root@PC1:/home/test/test# cat a.txt
    w
    s
    g
    d
    w
    a
    root@PC1:/home/test/test# sort -u a.txt > a && mv a a.txt
    root@PC1:/home/test/test# cat a.txt
    a
    d
    g
    s
    w
    root@PC1:/home/test/test# cat b.txt
    d
    a
    e
    d
    t
    c
    root@PC1:/home/test/test# sort -u b.txt > a && mv a b.txt
    root@PC1:/home/test/test# cat b.txt
    a
    c
    d
    e
    t

    3、测试使用

    root@PC1:/home/test/test# cat a.txt
    a
    d
    g
    s
    w
    root@PC1:/home/test/test# cat b.txt
    a
    c
    d
    e
    t
    root@PC1:/home/test/test# comm a.txt b.txt
                    a
            c
                    d
            e
    g
    s
            t
    w

    第一列是a.txt中唯一的项, 第二列是b.txt中唯一的项, 第三列是a.txt和b.txt中共有的项

    4、只保留a.txt中唯一的项、 或者b.txt中唯一的项、或者两者交集

    root@PC1:/home/test/test# cat a.txt
    a
    d
    g
    s
    w
    root@PC1:/home/test/test# cat b.txt
    a
    c
    d
    e
    t
    root@PC1:/home/test/test# comm a.txt b.txt
                    a
            c
                    d
            e
    g
    s
            t
    w
    root@PC1:/home/test/test# comm a.txt b.txt -2 -3
    g
    s
    w
    root@PC1:/home/test/test# comm a.txt b.txt -1 -3
    c
    e
    t
    root@PC1:/home/test/test# comm a.txt b.txt -1 -2
    a
    d

    5、取两者并集

    root@PC1:/home/test/test# cat a.txt
    a
    d
    g
    s
    w
    root@PC1:/home/test/test# cat b.txt
    a
    c
    d
    e
    t
    root@PC1:/home/test/test# comm a.txt b.txt
                    a
            c
                    d
            e
    g
    s
            t
    w
    root@PC1:/home/test/test# comm a.txt b.txt  | sed 's/^[\t]\+//g'
    a
    c
    d
    e
    g
    s
    t
    w
  • 相关阅读:
    【转】Shell编程基础篇-上
    【转】inotify+rsync实现实时同步
    Spring
    jdk,jre,tommcat配置问题
    Java前后台开发
    前端组件学习(一)
    报表工具进阶(二)
    查询时异步刷新问题--用到了ajax
    学习jaspersoft/JasperReport
    利用SQLYog操作数据库mysql
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15450665.html
Copyright © 2020-2023  润新知