【发布时间】:2014-04-24 23:20:51
【问题描述】:
FileA 包含行 FileB 包含单词
我如何有效地从 FileB 中删除包含在 FileA 中找到的单词的行?
我尝试了以下方法,但我什至不确定它们是否有效,因为它需要很长时间才能运行。
试过grep:
grep -v -f <(awk '{print $1}' FileB.txt) FileA.txt > out
也试过python:
f = open(sys.argv[1],'r')
out = open(sys.argv[2], 'w')
bad_words = f.read().splitlines()
with open('FileA') as master_lines:
for line in master_lines:
if not any(bad_word in line for bad_word in bad_words):
out.write(line)
文件A:
abadan refinery is one of the largest in the world.
a bad apple spoils the barrel.
abaiara is a city in the south region of brazil.
a ban has been imposed on the use of faxes
文件B:
abadan
abaiara
期望的输出:
a bad apple spoils the barrel.
a ban has been imposed on the use of faxes
【问题讨论】:
-
你的文件有多大?
-
FileAwith lines 有3M 行,FileBwith the keywords 约200k -
@user1899415 你能从两个文件的样本数据中发布吗?另外,请确保您的文件没有 Windows 格式。您可以使用
dos2unix实用程序将它们转换为。 -
@jaypal 已编辑以包含示例数据和所需的输出
-
将 FileB 加载到 python
set或dict将允许您进行更快的查找,所以我希望如果您这样做,结果会更好。顺便说一句,您在示例中混合了 FileA 和 FileB。