[wellylug] perl recursive text search and replace
Mark Signal
mark at databackup.co.nz
Sat Aug 21 13:04:40 NZST 2004
This was my final working version
all improvements gratefully accepted
#!/bin/bash
OLDTEXT=orderinfo/order.htm
NEWTEXT=stock_find.php
SEARCHDIR=html/
FILETYPE=*.htm*
find $SEARCHDIR -type f -iname $FILETYPE -exec perl -pi -e
"s{$OLDTEXT}{$NEWTEXT}g" '{}' ';'
speed hasn't appeared to be a problem as I am only dealing with a hundred or
so files with a few hundred lines per file (a biggish web site)
cheers
Mark
-----Original Message-----
From: wellylug-admin at lists.naos.co.nz
[mailto:wellylug-admin at lists.naos.co.nz]On Behalf Of David Antliff
Sent: Friday, 20 August 2004 11:33 a.m.
To: wellylug at lists.naos.co.nz
Subject: Re: [wellylug] perl recursive text search and replace
On Fri, 20 Aug 2004, Darryl Hamilton wrote:
> I don't think Perl can do that kind of thing in a one-liner, but I'm not
> exactly experienced with it, so I'm probably wrong.
You can use the File::Find module if you wanted to but it would get messy
as a one-liner.
> I'd use a 'find -exec' set up to handle the sub directories
>
> find . -name *.txt -type f -exec perl -w -pi -e "s/oldinfo/newinfo/g;" {}
\;
As a matter of interest, find | xargs can fail unusually if you aren't
careful. Rather than use 'find | xargs cmd' you're better off using 'find
-print0 | xargs -0 cmd' - this will use null terminators to delimit
filenames rather than spaces, which means it will handle filenames with
spaces properly (since find doesn't inject escapes). I'm not sure how to
handle filenames with other shell characters like quotes however, but you
could always escape everything I guess...
That find -exec command will execute a new perl process for each result,
won't it? With the overhead of byte-compiling perl wouldn't this be
considerably slower for large result sets (O(N) processes) than using find
| xargs (O(2) processes) ?
--
David.
--
Wellington Linux Users Group Mailing List: wellylug at lists.naos.co.nz
To Leave: http://lists.naos.co.nz/mailman/listinfo/wellylug
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.742 / Virus Database: 495 - Release Date: 19/08/2004
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.742 / Virus Database: 495 - Release Date: 19/08/2004
More information about the wellylug
mailing list