in reply to Modifying A Bunch of Files by replacing specific lines
Update: upon closer inspection, I see my answer is similar to BrowserUK's above, though my answer also deals with actually replacing the file, not just outputting the replaced text, and should work within a File::Find solution.use File::Temp qw(tempfile); use Cwd; sub replace_file { my ($file, $start, $end, $replace_text) = @_; local (*ARGV, $_, $.); @ARGV = $file; my ($fh, $tmp_file) = tempfile(DIR=>cwd); my $replaced; while (<>) { my $replace = /\Q$start\E/../\Q$end\E/; print $fh $_ and next unless $replace; $replaced=1; print $fh $replace_text if $replace == 1; } close $fh; return unless $replaced; rename $tmp_file, $file or warn "Error replacing $file: $!"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Modifying A Bunch of Files by replacing specific lines
by ishaqali (Novice) on Feb 26, 2003 at 07:53 UTC |