[wellylug] perl recursive text search and replace
David Antliff
dave.antliff at paradise.net.nz
Fri Aug 20 11:32:59 NZST 2004
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.
More information about the wellylug
mailing list