my $filename = "../../Annotation_output"; for (my $i=0; $i<=33; $i++) { my $file_count = $i; open(my $new_fh, '>>', ("$filename" . "$file_count" . ".vcf")); my $count = 0; while (<$fh>) { print $new_fh "$_"; $count++; if ($count == 16) { last; } } }