#!/usr/local/bin/perl # multifileout.pl use strict; use warnings; my @file_ends = qw{./ .fasta}; my %handle = (); my @ko_array = qw(abc def ghi); while () { chomp; my ($head, $seq) = split / /; for my $ko (@ko_array) { if ($head =~ m/$ko/) { if (! exists $handle{$head}) { open my $fh, q{>>}, join($ko, @file_ends); $handle{$head} = $fh; } print { $handle{$head} } qq{$head\n$seq\n}; } } } map { close $handle{$_} } keys %handle; __DATA__ abc a123 def d123 abc a456 ghi g123 ghi g456 def d456