I'd use Python for this. Put all this code into a file called mass_replace and "chmod +x mass_replace
":
#!/usr/bin/python
import os
import re
import sys
def file_replace(fname, s_before, s_after):
out_fname = fname + ".tmp"
out = open(out_fname, "w")
for line in open(fname):
out.write(re.sub(s_before, s_after, line))
out.close()
os.rename(out_fname, fname)
def mass_replace(dir_name, s_before, s_after):
for dirpath, dirnames, filenames in os.walk(dir_name):
for fname in filenames:
f = fname.lower()
# example: limit replace to .txt, .c, and .h files
if f.endswith(".txt") or f.endswith(".c") or f.endswith(".h"):
f = os.path.join(dirpath, fname)
file_replace(f, s_before, s_after)
if len(sys.argv) != 4:
u = "Usage: mass_replace <dir_name> <string_before> <string_after>\n"
sys.stderr.write(u)
sys.exit(1)
mass_replace(sys.argv[1], sys.argv[2], sys.argv[3])
For a single search and replace of one string in one type of file, the solution with find and sed isn't bad. But if you want to do a lot of processing in one pass, you can edit this program to extend it, and it will be easy (and likely to be correct the first time).
Best Answer
The problem with editing binary files is that they are often laid out in a particular format with the position of particular bytes having significance. So trying to automate that can be very difficult and should probably be done with a tool that understands the format of the file.
The following Bash script can be used to edit text files:
or change the second and last lines to:
and
If your version of
sed
can do in-place editing, then you can eliminate the creation of temporary files (everything betweendo
anddone
above) and use thissed
command inside the loop instead: