use strict; use warnings; my $inFileStr = <) { chomp; my @data = split /,/; if ($data[2] =~ /^(D|C)$/) { print " > $first.txt\n" if defined $first; print "type $data[2] "; $first = $data[1]; } print "$data[1].txt "; } close $inFile; print " > $first.txt\n" if defined $first; #### type D BEN01.txt > BEN01.txt type D BEN02.txt BEN03.txt BEN04.txt BEN05.txt > BEN02.txt type C BEN06.txt BEN07.txt BEN08.txt BEN09.txt > BEN06.txt type D BEN10.txt > BEN10.txt