my $rpow; for $rpow (@remove_power_list) { my $bf_content; open $bf_content, '+<', $bf_bind; my $count = 0; while (<$bf_content>){ if($_ =~ /Power\s+information\s+for\s+$rpow\s+/) { $count = $. + 11; } else { if($count != 0 && $. < $count) { } elsif($count != 0 && $. == $count) { $count =0; } else { print $bf_content $_; } } } close $bf_content; }