【问题标题】:Remove lines from file if the first column appears in another file如果第一列出现在另一个文件中,则从文件中删除行
【发布时间】:2016-08-29 20:24:26
【问题描述】:

我必须像这样的文件:

文件1:

id1 junk junk junk 

id2 junk junk junk 

id3 junk junk junk 

id4 junk junk junk

文件2:

id2

id3

如何从 File1 中删除在 File2 中具有 id 的 2 行。所以输出将是

id1 junk junk junk

id4 junk junk junk

谢谢

【问题讨论】:

    标签: bash unix terminal


    【解决方案1】:

    使用 awk 你可以做到这一点:

    awk 'FNR==NR{a[$1]; next} !($1 in a)' file2 file1
    
    id1 junk junk junk
    id4 junk junk junk
    

    或将grep -v 与进程替换一起使用:

    grep -vf <(sed 's/.*/^&[[:blank:]]/' file2) file1
    
    id1 junk junk junk
    id4 junk junk junk
    

    【讨论】:

    • 非常感谢。 awk 太强大了。
    【解决方案2】:

    您可以告诉 grep 从 file2 中排除所有内容

    grep -vf file2 文件1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 2015-01-01
      • 2012-06-28
      • 2011-06-14
      • 1970-01-01
      • 2017-07-22
      • 2015-02-04
      • 2015-12-21
      相关资源
      最近更新 更多