#initialize some variables my $records; # No. of records my $last; # The last line my $ini = 1; # set initial line my $count = 1; # counter for (my $count = 1; $count <= 20; $count++){ open (OLD, "< $old_file.txt") || die $!; # open file # Now we need to split up the file into 20 files open (NEW, "> $old_file$count.txt") || die $!; #open outputfile while (){ if($. == $ini){ chomp; $records = $_; } } close OLD; $last = $ini + $records + 1; open (OLD, "<$old_file.txt") || die $!; # I am more comfortable with re-opening the file like this while (){ next unless $. >= $ini; print NEW $_; last if $. >= $last; } close OLD; close NEW; $ini = $last + 1; }