use strict; use warnings; my ($mio, $mio2); my @array_with_all_fields=(); my $match = 'DOID:2055'; unlink ("myoutputfilename6.txt"); unlink ("myoutputfilename7.txt"); open(my $file, '<', 'do_human_mapping.gmt') or die "open: $!"; open my $out_file3, '>', 'myoutputfilename6.txt' or die "$!"; open my $out_file4, '>', 'myoutputfilename7.txt' or die "$!"; while (<$file>){ my ($name,$id,@genes) = split /\t/; if ($id eq $match) { $mio= join("\n",@genes); print $mio."\n"; print $out_file3 $mio."\n"; # print on file } if (grep/^$match$/, @genes){ $mio2=$id; print $mio2."\n"; print $out_file4 $mio2."\n"; # print on file } }