#!/usr/bin/perl use strict; use warnings; my $filename = '16lines.txt'; open (INFILE, '<', $filename) or die "unable to read the file with 16+ lines $!"; my @lines = ; #read all lines into an array for my $file_no (0..33) #total 34 files?? Really? Maybe 0..31? { open (my $fh, ">", "$filename$file_no.vcf") or die "problem opening $filename$file_no.vcf Oops $!"; print $fh grep{defined}@lines[0..15]; # the grep on defined lines is in case there are less # 16 lines in the input file. I am sure there are other # with perhaps split() to accomplish the same thing. }