Created
May 23, 2010 05:17
-
-
Save matthew-brett/410660 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 | |
''' Tiny script to do perl -p -i -e -type replace on text in files ''' | |
import sys | |
import re | |
def perl_dash_pie(in_exp, out_str, filename): | |
in_reg = re.compile(in_exp) | |
in_txt = open(filename, 'rt').read(-1) | |
out_txt = in_reg.sub(out_str, in_txt) | |
if in_txt != out_txt: | |
open(filename, 'wt').write(out_txt) | |
return True | |
return False | |
usage = ('Usage: ' | |
'%s <str_to_replace> <replacement> <filename0> ' | |
'[<filename1> ...]') | |
if __name__ == '__main__': | |
prog = sys.argv.pop(0) | |
if len(sys.argv) < 3: | |
raise OSError(usage % prog) | |
in_reg = re.compile(sys.argv.pop(0)) | |
out_str = sys.argv.pop(0) | |
for fname in sys.argv: | |
if perl_dash_pie(in_reg, out_str, fname): | |
print 'Changes in file %s' % fname | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment