[Techtalk] Re: real life kewl text-processing and regexps samples wanted

Erin Mulder meara at alumni.princeton.edu
Sat Jan 10 14:25:22 EST 2004


Ricky Buchanan wrote:
> perl -pie 's/\bgossamer\b/rb/g' *.txt
> 
> will swap every instance of the word (as a word) "gossamer" to "rb" in
> *.txt in the current directory, making a backup of each file as
> originalfilename.bak (that's the -i) just in case of grevious error (and
> you do know how to use mmv to put the bak files back just in case,
> right?[1])

Cool.   I've got a shell script called "mangle":

    #! /bin/sh
    while read i; do
       sed "s|$1|$2|g" $i > $i.tmp && mv -f $i.tmp $i
    done

which I call like this:

    find . -type f -name \*.txt -print | mangle foo bar

but your perl snippet looks a lot cleaner and easier. :)

Cheers,
Erin


More information about the Techtalk mailing list