Created
August 6, 2015 23:09
-
-
Save jakeogh/ea48748417fec5f170ec to your computer and use it in GitHub Desktop.
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 | |
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