【发布时间】:2014-10-26 11:11:09
【问题描述】:
目的是从两个文件中提取相同的行,同时忽略小写/大写以及标点符号
我有两个文件
source.txt
Foo bar
blah blah black sheep
Hello World
Kick the, bucket
processed.txt
foo bar
blah sheep black
Hello world
kick the bucket ,
期望的输出(来自source.txt):
Foo bar
Hello World
Kick the, bucket
我一直这样做:
from string import punctuation
with open('source.txt', 'r') as f1, open('processed.txt', 'r') as f2:
for i,j in zip(f1, f2):
lower_depunct_f1 = " ".join("".join([ch.lower() for ch in f1 if f1 not in punctuation]).split())
lower_depunct_f2 = " ".join("".join([ch.lower() for ch in f2 if f2 not in punctuation]).split())
if lower_depunct_f1 == lower_depunct_f2:
print f1
else:
print
有没有办法使用bash 工具来做到这一点? perl、shell、awk、sed?
【问题讨论】: