Skip to content

Instantly share code, notes, and snippets.

@jakeogh
Created August 6, 2015 23:09
Show Gist options
  • Save jakeogh/ea48748417fec5f170ec to your computer and use it in GitHub Desktop.
Save jakeogh/ea48748417fec5f170ec to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import sys
line_pattern=sys.argv[1]
line_to_insert = sys.argv[2]
text = open( '/dev/stdin' ).read()
match_found = False
matches = re.finditer(line_pattern, text)
m = None # optional statement. just for clarification
for m in matches:
match_found = True
pass # just loop to the end
if (match_found):
m.start() # equals the starting index of the last match
m.end() # equals the ending index of the last match
# now you can do your substring of text to add whatever
# you wanted to add. For example,
print(text[0:m.end()] + "\n" + line_to_insert + text[(m.end()+1):])
#print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment