in reply to Editing Contents of a File
The simplest way is a one-liner:
You are reading the file and modifying an in-memory string, but not attempting to write that string back out. I would do this:perl -i.bak -pe 's/hello/goodbye/gi' file1 file2 ...
Update: Added the truncate call for cases where the modified file contents are shorter than the original. Oops.use Fcntl qw(:seek); open (FOO, "+<$foo") or die "Can't open $foo: $!"; my $slurp = join '', <FOO>; $slurp =~ s/hello/goodbye/sgi; seek FOO, 0, SEEK_SET; print FOO $slurp; truncate FOO, tell FOO; close FOO;
|
|---|