in reply to multi-file search-and-replace
update: tachyon makes a good point in his reply to this reply. If you think your data set may contain such a catch, make sure you follow his advice :)
I really shouldn't have written the whole thing for you, but whatever, I'm in a good and helpful mood. This could most likely be done in less code using the 'find' command in combination with perl's inline editting, but I don't know enough about such things. Note that I copped out by simply slurping each file into memory one at a time. You could rescript it to do line-by-line editting if you wish, though you'd have to add usage of a temporary file.
#!perl -w use strict; use File::Find; find( \&handle_it, '/foo/bar' ); sub handle_it { return unless m!\.html\z!; my $name = $File::Find::name; open( my $fh, '+<', $name ) or (warn("open on $name failed: $!\n") and return); my $content = do { local $/; <$fh> }; $content =~ s!foo\@there\.com!bar\@here\.us!g; seek($fh, 0, 0) or die("seek on $name failed: $!\n"); truncate($fh, 0) or die("truncate on $name failed: $!\n"); print $fh $content; close($fh) or die("close on $name failed: $!\n"); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: multi-file search-and-replace
by tachyon (Chancellor) on Jun 17, 2004 at 00:35 UTC |