考虑我有两个文本文件。
第一个文件名-“ Emails.txt”,其中包含以下数据:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
第二个文本文件-“ Banned.txt”,带有以下字符串:
@gotmail.com
@cmail.com
@uor.edu
如果第一个文本文件“ Emails.txt”中的所有行与第二个文本文件“ Banned.txt”中存在的任何行的字符串匹配,该如何删除?
新文件的期望输出应为:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
可以在Linux中使用SED或awk完成此操作吗?你能建议如何做到这一点吗?
grep -v
足够的。该标志-f
使您可以完全按照自己的意愿进行操作:
grep -vf Banned.txt Emails.txt
如果您想从被禁止的地址列表中做一些更复杂的事情,例如强加它们与整个域匹配,则需要从Banned
文件中生成一个正则表达式:
cat Banned.txt | tr "\n" "|" | sed -e 's,|,$\\|,g' | sed -e 's,\\|$,,'
给出期望的
@gotmail.com$\|@cmail.com$\|@uor.edu$
然后:
cat Banned.txt | tr "\n" "|" | sed -e 's,|,$\\\\|,g' | sed -e 's,\\|$,,' | xargs -i grep -v '{}' Emails.txt
(\
通过进行评估时,转义次数增加了一倍xargs
)。这将匹配并删除,[email protected]
但不会匹配[email protected]
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句