in reply to Modifying A Bunch of Files by replacing specific lines

This assumes your markers are the only text on the line (or at least the only text you care about). You'll have to adjust and do some extra 's///' if that's not the case:
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: $!"; }
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.

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
    Thank you all for the great help. The markers are not the only text on the line. For example one of the file has these lines: "<\td class="health" height="99%"> <-- #BeginEditable "category" class="category" -->MEDICAL {carriage return} BENEFITS</\td></\tr> <\tr valign="top"> <\td height="99%" class="mainbody"> " (The extra slashes are to display the html code as is) The beginning marker for this is <-- #BeginEditable "category" class="category" --> and the ending marker for this is <-- #EndEditable --> i.e. I have to discard everthing after the beginning marker but not the marker or any thing before itself. I have to discard everything before the end marker but not the end marker or anything after that. I think I will try figuring this out myself. If anybody can find some time to post the code, I would be greatful. Thank You for the great help.