my @output; my $outfile_name = "Outfile0000"; while ( ) { chomp; push @output, $_; if( /VECT/ or $. % 12 == 0 or eof(DATA) ) { open my $out_fh, '>', $outfile_name++ or die $!; say $out_fh $_ for @output; @output = (); close $out_fh or die $!; } }