【问题标题】:How to remove lines contained in file 1 from file 2 if in file 2 they are prefixed?如果文件 2 中有前缀,如何从文件 2 中删除文件 1 中包含的行?
【发布时间】:2017-04-01 21:43:36
【问题描述】:

我有以下情况:

source.txt

ID1:email1@domain1.com
ID2:email2@domain2.com
ID3:email3@domain3.com
...

ID 是数字字符串,例如123423412897...(一位或多位数字)。

exclude.txt

emailX@domainX.com
emailY@domainY.com
emailZ@domainZ.com
...

即只有电子邮件,没有 ID。

我想从 source.txt 中删除所有包含 exclude.txt 中列出的电子邮件的行,为未删除的行保留 ID:email 对。

如何使用 linux 命令行工具(或简单的 bash 脚本,如果需要)来做到这一点?

【问题讨论】:

    标签: linux shell grep


    【解决方案1】:

    您可以使用 awk 轻松完成:

    awk -F":" 'NR==FNR{a[$1];next}(!($2 in a))' exclude.txt source.txt
    

    用 grep 替代:

    grep -v -F -f exclude.txt source.txt
    

    小心使用 grep,因为 grep 会进行正则表达式匹配。您可能还需要在 grep 中添加 -w 选项(单词匹配)

    【讨论】:

    猜你喜欢
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多