use strict; use warning; open IN, 'result.txt' or die "Error opening result.txt: $!"; local $/ = undef; $_ = ; close IN; # this is the multi line text to be searched for my $pattern=" the patten is in many line and @"; # after the match, there is some multi line text to be inserted, which I don't know how to. if(/\Q$pattern/){ splice @array, $_ +1, 0, "inserting pattern in many lines with spaces "; } open OUT, '>', 'result.txt' or die "Error opening result.txt: $!"; print OUT $_; close OUT;