[vox-tech] Using awk or perl to find and replace

Mitch Patenaude mrp at sonic.net
Wed Nov 24 10:15:45 PST 2004


On Nov 24, 2004, at 9:56 AM, Foo Lim wrote:
> Add an i to the end of the statement:
>
> s/h\.(.*?)\.JPG/$1.h.JPG/gi;
>
> However, this will match files that start with a lowercase h as well 
> as an
> uppercase H.  If that's fine, then this regex will do the job.

No... the problem is that it will take

   h.foo1.jpg
and replace it with
   foo1.h.JPG
Which probably won't work (since the case of the extension changes and 
most *nix are case sensitive...)  The regex should be

s/h\.(.*?)\.(JPG|jpg)/$1.h.$2/g;

Or even more generally:

s/h\.(.*?)\.([Jj][Pp][Gg])/$1.h.$2/g;

Which you can wrap up in a single command like so

perl -pi.orig -e 's/h.(.*?).([Jj][Pp][Gg])/$1.h.$2/g;' *.html

which will do the replacement on all HTML files.. saving the originals 
with the extension .orig in case something goes horribly wrong and your 
need to "undo" it.

   -- Mitch



More information about the vox-tech mailing list