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

Here's an example as a first shot at this:
#!/usr/local/bin/perl # repl.pl use strict; open INPUT, "<input.txt" or die "Blerch: $!\n"; open OUTPUT, ">output.txt" or die "Hcrelb: $!\n"; my $replace_string="Hi there.\n"; my $flag=0; my %markers=("BEGIN" => "<!- Begin Replacable Section-->", "END" => "<!- End Replacable Section-->", ); while (<INPUT>) { if ($flag) { next unless /$markers{END}/; $flag=0; print OUTPUT $replace_string; } print OUTPUT; $flag=1 if /$markers{BEGIN}/; } __END__ > cat input.txt But now I know that twenty centuries of stony sleep were vexed to nightmare by a rocking cradle. And what rough beast, it's hour come round at last, slouches towards Betlehem to be born? <!- Begin Replacable Section--> A poem <!- End Replacable Section--> W. B. Yeats > repl.pl ; cat output.txt But now I know that twenty centuries of stony sleep were vexed to nightmare by a rocking cradle. And what rough beast, it's hour come round at last, slouches towards Betlehem to be born? <!- Begin Replacable Section--> Hi there. <!- End Replacable Section--> W. B. Yeats

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Modifying A Bunch of Files by replacing specific lines
by dash2 (Hermit) on Feb 20, 2003 at 09:48 UTC
    You'll probably want to do this:

    next unless /\Q$markers{END}\E/;

    and the same for the other pattern match. This ensures that your markers string gets seen as a literal string to match, not as a pattern itself. Especially if you are using < and > which tend to have meanings within regexes.

    dave hj~