zoukankan      html  css  js  c++  java
  • 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=<字符位置>:指定要比较的字符。
  • 相关阅读:
    2020.10.08 模拟赛 题解
    可爱路径 题解
    飞行路线 题解2
    架设电话线 题解
    浅谈 LCA
    浅谈 Tarjan 算法之强连通分量(危
    初赛题目整理1
    【求助,待续!】holes
    【题解】HDU4467
    【题解】zhx154嬲
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/9535774.html
Copyright © 2011-2022 走看看