in reply to The best way to replace several fragments of the file starting with the one pattern(marker) and ending with another pattern/marker.

One possibility:
#!/usr/bin/perl use strict; use warnings; my %edits = ('AAAAAA' => 'BBBBBB f11 f12 f13', 'CCCCCC' => 'DDDDDD f21 f22'); my $line; while ($line = <DATA>) { print $line; chomp $line; process_section ($edits{$line}) if exists $edits{$line};; } sub process_section { my $input = shift; my ($end_token, @values) = split (' ',$input); foreach (@values) {print "$_\n"} my $this_line; while (defined ($this_line = <DATA>) and $this_line !~ /$end_token$ +/) { next; } print "$end_token\n"; } __DATA__ aaa aaa AAAAAA ccc ddd BBBBBB 111 222 333 CCCCCC 444 555 666 DDDDDD 777 888 999 =PRINTS: aaa aaa AAAAAA f11 f12 f13 BBBBBB 111 222 333 CCCCCC f21 f22 DDDDDD 777 888 999
update: There is flaw in this. Adjacent sections will not process correctly. However this approach, with modifications, scales to large files.
  • Comment on Re: The best way to replace several fragments of the file starting with the one pattern(marker) and ending with another pattern/marker.
  • Download Code