Skip to content

Instantly share code, notes, and snippets.

@manicminer
Last active August 29, 2015 14:11
Show Gist options
  • Save manicminer/df493f01a6d7173ff6f5 to your computer and use it in GitHub Desktop.
Save manicminer/df493f01a6d7173ff6f5 to your computer and use it in GitHub Desktop.
Append some string in place after a multiline match
#!/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()
$ ./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