Skip to content

Instantly share code, notes, and snippets.

@gregturn
Created July 19, 2013 13:52
Show Gist options
  • Save gregturn/6039247 to your computer and use it in GitHub Desktop.
Save gregturn/6039247 to your computer and use it in GitHub Desktop.
This script will parse a markdown file and replace any lines that look like <@snippet path="pom.xml" prefix="complete"/> with <@snippet path="pom.xml" prefix="initial"/>. It writes the new files into <file>.new first, and then moves it to replace the existing file. See comments for more details
#!/usr/bin/env python
import os
import re
import sys
if len(sys.argv) < 2:
print "Usage: %s <file>" % sys.argv[0]
sys.exit(1)
with open(sys.argv[1]) as input:
with open(sys.argv[1] + ".new", "w") as output:
for line in input.readlines():
if "snippet" in line and "pom.xml" in line and "complete" in line:
output.write(re.sub("complete", "initial", line))
else:
output.write(line)
os.rename(sys.argv[1]+".new", sys.argv[1])
@gregturn
Copy link
Author

This script will parse a markdown file and replace any lines that look like <@snippet path="pom.xml" prefix="complete"/> with <@snippet path="pom.xml" prefix="initial"/>. It writes the new files into <file>.new first, and then moves it to replace the existing file.

USAGE: fix <file>. Very handy inside a loop, like for repo in $(find . -name .git); do cd $(dirname $repo); pwd; fix README.ftl.md; cd ..; done;

REUSE: Edit the if blocks to alter the pattern you seek as well as the substitution pattern. Before committing to a final version, comment out os.rename and diff <file> <file>.new to see if it's what you want. Put the script somewhere like /usr/local/bin so it's on your path.

RECOMMEND: Only use with version controlled files so you can back out changes if need be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment