Skip to content

Instantly share code, notes, and snippets.

@ithayer
Last active December 15, 2015 11:59
Show Gist options
  • Save ithayer/5257033 to your computer and use it in GitHub Desktop.
Save ithayer/5257033 to your computer and use it in GitHub Desktop.
Simple python script to regex replace the contents of a file.
#!/usr/bin/env python
import os
import re
from optparse import OptionParser
parser = OptionParser()
(options, args) = parser.parse_args()
def eatFile(tmplfile):
print "Eating %s" % tmplfile
# Read file contents.
f = open(tmplfile)
z = f.read()
f.close();
# Replace.
z = re.sub('^\s*(\d.\d)([A-z])', r"\1 \2", z, 0, re.MULTILINE)
# Rewrite file contents.
f = open(tmplfile, "w")
f.write(z);
f.close();
for f in args:
eatFile(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment