python - Conditionally remove lines between two patterns in a file -
input file :
<chunk of text> pattern1 abc efg hij pattern2 klm nop pattern3 <chunk of text>
output file :
<chunk of text> <chunk of text>
how remove lines between pattern 1 , pattern 3 of file(inclusive) if there pattern2 between them
import sys,re flag,delete,store = false,false,"" line in open(sys.argv[1]): if re.search('pattern1',line): flag = true print store store = line continue if flag : store += line if re.search('pattern2',line): delete = true if re.search('pattern3',line) : if not delete : print store store = '' flag = false delete = false else : print line, print store
Comments
Post a Comment