use strict; my @superchunk; # i think i'm hyper enough as it is ... my $i = 0; open(IN,'output.txt') or die $!; # process one line at a time # add each line to the element that $i is # increment $i when TIMESTEP is encountered while () { $superchunk[$i] .= $_; $i++ if /^TIMESTEP/; } close IN; $i = 1; # process one element at a time # use sprintf to create the name of the file # write that element to the file for (@superchunk) { my $file = sprintf("%s%02d%s",'BRO',$i++,'.pdb'); open(OUT,">$file") or die "can't write to $file: $!"; print OUT $_; }