Thursday, September 6, 2007

search and replace in files

perl -pi -w -e 's/search/replace/g;' *

with multi-line strings:

import sys

search = 'search over
multiple lines
'

replace = 'replace with
this string
'

for i in sys.argv[1:]:
f = open(i, 'r')
s = f.read()
f.close()

f = open(i, 'w')
f.write(s.replace(search, replace))
f.close()

No comments: