#!/usr/bin/env python #--------------------------------------------------- # This program searches for a word in list of files # and replace it with another word. #--------------------------------------------------- import os import sys import re def usage(msg): sys.stderr.write("This script attempts to search a word in a provided\n") sys.stderr.write("list of files and replace it with another word.\n") sys.stderr.write("\n ") sys.stderr.write("Usage:\n") sys.stderr.write(" %s %s\n" % (sys.argv[0], msg)) sys.exit(1) n = len(sys.argv[1:]) if (n < 3): usage('oldWord newWord file1 file2 file3 ...') #----------------------------------- # Get the words from the command line #----------------------------------- oldWord = str(sys.argv[1]) newWord = str(sys.argv[2]) #------------------------------------------- # Start the search by looping over the files #------------------------------------------- for fName in sys.argv[3:]: data = open(fName).read() open(fName,"w").write( re.sub(oldWord,newWord,data) )