regex - Python search string and replace character in next line -


in text file want search line containing specific text , @ next line want substitute beginning of line #(comment out) inplace:

example - before:

#test search 123 text commented out #test 123 

example - wanted:

#test search 123 #text commented out #test 123 

i can via sed:

sed -i '/^#test search 123/!b; n; s/^/#/' test_file 

but wondering if i'm able natively in python.

import os  outfile = open('bla.txt.2', 'w')  search = "#test search 123" flag = 0  open('bla.txt', 'r') f:      line in f:         if flag == 1:             mod_line = "#" + line             outfile.write(mod_line)             flag = 0             continue          outfile.write(line)         if (search in line):             flag = 1  outfile.close()  os.remove('bla.txt') os.rename('bla.txt.2', 'bla.txt') 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -