It's probably easier to do this using Python's built in regex module, re
, rather than shelling out to grep
.
import re
with open(filename, 'r') as f:
for line in f:
if re.search(regex, line):
print line,
I know this doesn't answer the question directly but it's probably the right solution to the underlying problem. I think this falls into the "is there a better way to do this?" part of your question.