How do you delete a lines within a file where range is determined using regular expressions in python?
I am trying to clean up a series of text files by deleting unneeded lines. I want to delete lines starting with the line that matches Regex1 and keep deleting until the line with Regex2 is found. I need to do this using python. I have already done this using a sed command something like
sed -r '/regex1/,/regex2/d'
and had good luck, but I need something I can run on Windows machines. Plus, I want the whole solution to 开发者_如何学编程be using one language instead of piping between shell and python scripts. Anyone have any ideas or posts to point me to?
You can do this by using re.DOTALL
to make .
match newlines:
import re
pattern = re.compile(r"regex1.*?regex2", re.DOTALL)
You can then use pattern.sub("", data)
to delete the lines.
import sys,re
reg_start = 'reg1'
reg_end = 'reg2'
to_print = 1
for line in sys.stdin.readlines():
if re.search(reg_start, line):
to_print = 0
elif re.search(reg_end, line):
to_print = 1
if to_print:
print line,
In perl there's flip-flop operator that can be use as :
perl -ne "print unless /regex1/ .. /regex2/" file
you could convert this to python as it's explain here
精彩评论