What I want to do is (using the text below as an example), search for the string “Text2” in a text file, and then insert a line (“Inserted Text”) two lines after \"Text 2\".
The quick-n-dirty way would be something like that
before=-1
for line in fileinput.input('file.txt', inplace=1,backup='.bak'):
if line.startswith('Text 2'):
before = 2
if before == 0
print "Inserted Text"
if before > -1
before = before - 1
print line,