【问题标题】:How to find and delete duplicate lines in two files in vi editor or linux如何在vi编辑器或linux中查找和删除两个文件中的重复行
【发布时间】:2013-12-27 19:20:37
【问题描述】:

我有 2 个文件 A 和 B。它们都有超过 100 行。我想删除两个文件之间的公共线。
答:

asdf123
fdsa123
rrrr456
yyyy555

乙:

fdsa123
hhhh888
yyyy555

所以现在文件应该是
A1:

asdf123
rrrr456

B1:

hhhh888

【问题讨论】:

    标签: linux sed awk grep vi


    【解决方案1】:

    您可以为此使用grep

    $ grep -vf a b    # or   grep -vf a b > b1   to save it
    hhhh888
    
    $ grep -vf b a    # or   grep -vf b a > a1   to save it
    asdf123
    rrrr456
    

    请注意,grep -f file1 file2 会检查 file1 中出现在 file2 中的行。即从file1获取模式。

    那么,grep -v 代表反转匹配。即得到与grep 命令相反的结果。

    【讨论】:

    • 值得注意的是,默认情况下 grep 匹配带有正则表达式的行,这在这种情况下是不需要的。您需要添加 -F 参数,以便 grep 将行比较为纯字符串:grep -Fvf file1 file2
    【解决方案2】:

    你可以试试:

    awk -f del.awk fileA fileB > fileB.del
    awk -f del.awk fileB fileA > fileA.del
    

    del.awk 在哪里

    NR==FNR {
        a[$0]++
        next
    }
    
    ! ($0 in a) {
        print
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 2011-03-08
      • 1970-01-01
      • 2015-02-25
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      相关资源
      最近更新 更多