while (my $line2 = ) { if ($line2 =~ m/(?=Block \d+: .*)/ .. $line2 =~ m/______________/){ push (my @text, $line2); foreach $text(@text) { $text =~s/Block \d+: .*//; $text =~s/\n//; } }