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.


In reply to Re: Search and Replace Entire Directories by kwoff
in thread Search and Replace Entire Directories by CiceroLove

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.