use strict; use warnings; my $idsfile = "sample_IDs.txt"; my $seqfile = "sample_reads.fasta"; my %ids = (); open (my $idfh, "<", $idsfile) or die "Can't open $idsfile - ", $!, "\n"; while(my $line = <$idfh>) { chomp $line; next unless $line; my @contents = split /\s/, $line; $ids{$contents[0]} += 1; } close $idfh; local $/ = "\n>"; # read by FASTA record open (my $fastafh, "<", $seqfile) or die "Can't open $seqfile - ", $!, "\n"; while (my $line = <$fastafh>) { chomp $line; next unless $line; my ($id) = $line =~ /^(\S+)/; # parse ID as first word in FASTA header next unless $id; if ($ids{">" . $id}) { $line =~ s/^>*.+\n//; # remove FASTA header $line =~ s/\n//g; # remove endlines print "$line\n"; } } close $fastafh;