Python delete lines of text line #1 till regex -
Python delete lines of text line #1 till regex -
i have issue can't seem find solution within python.
from command line can by: sed '1,/commands/d' /var/tmp/newfile
this delete line #1 till regex "commands". simple
but can't same python can find. re.sub , multiline doesn't seem work.
so have question how can in pythonic way? rather not run sed within python unless have to.
i utilize fileinput inplace=true
alter original file setting flag when find match , removing lines until do:
import fileinput import re found = false line in fileinput.input("your_file",inplace=true): if re.match("pattern",line): found = true if found: print(line,end="") # print line, python2 else: print(end="") # print, python2
if there nil complex pattern if "pattern" in line
may work.
python regex
Comments
Post a Comment