in reply to Search and Replace Entire Directories

I'm glad to see these kinds of solutions, because I also maintain a big, badly-designed site, with some HTML designers who usually don't design their pages with maintenance in mind. Like, they should use server-side includes more, but it requires extra effort to create a separate file and #include that. Then if they want to change something, they'll open up the page in their browser, edit the resulting HTML, and overwrite the nicely done page with a hard-coded copy. By the time I know about it, that page has been copied like a template to dozens of other (unknown files). ARGH..

Anyway, I also have similar problems, which I use the following for (it's not quite the same as yours and I usually end up tweaking it). I just offer it as another solution. Sorry it's not completely in perl. :)

#!/bin/sh # query-replace files recursively, substituting in place STRING1=$1 STRING2=$2 find . -type f | xargs -n 255 perl -p000i.bak -e "s/${STRING1}/${STRIN +G2}/g"

And a little explanation:

`find` recursively descend the file system, and its argument "-type f" means get only look at text files (sometimes I insert "-name '*.html'". The output of `find` is a list of filenames, which are piped to `xargs`. I use `xargs` for efficiency, so that you're not starting and stopping the `perl` interpreter hundreds of times. "-n 255" shovels only 255 file names at a time, though, so that the maximum command-line arguments of the shell aren't exceeded. Finally, those files are passed as arguments (by `xargs`) to `perl`. The -p switch prints every line, but in this case each line is a "paragraph" because of the -0 switch using '00' special case end-line delimiter (I did that so you could match regexps over multiple lines). The -i switch edits "in place", with each file backed up to a file with '.bak' appended to the name.

Example usage:

$ query-replace.sh '\<(\/)?[hH]3\>' '\<${1}H1\>'

would take <H3> or </h3> and replace with <H1> and </H1>, respectively. It's a bit ugly, I admit. I end up using emacs usually.