Last active
August 29, 2015 14:11
-
-
Save manicminer/df493f01a6d7173ff6f5 to your computer and use it in GitHub Desktop.
Append some string in place after a multiline match
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re, sys | |
filename = sys.argv[1] | |
match = sys.argv[2] | |
append = sys.argv[3] | |
f = open(filename, 'r+') | |
content = f.read() | |
new = re.sub('(%s)' % match, r'\1\n%s' % append, content, 0, re.MULTILINE) | |
f.write(new) | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./append.py /path/to/file '^find these\ntwo lines$' 'and append\ntwo more' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment