http://qs321.pair.com?node_id=11123326


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.

TIMTOWTDI

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11123325 use warnings; print do { local $/; <DATA> } =~ s/AAAAAA\n\K.*?(?=BBBBBB)/<<'END_OF_REPLACE'/gesr =~ f11 f12 f13 END_OF_REPLACE s/CCCCCC\n\K.*?(?=DDDDDD)/<<'END_OF_REPLACE'/gesr; f21 f22 END_OF_REPLACE __DATA__ aaa aaa AAAAAA ccc ddd BBBBBB 111 222 333 CCCCCC 444 555 666 DDDDDD 777 888 999

Outputs:

aaa aaa AAAAAA f11 f12 f13 BBBBBB 111 222 333 CCCCCC f21 f22 DDDDDD 777 888 999