with data.txt and all.txt as given this produces:use strict; use warnings; use Data::Dumper; use Fatal qw[open]; my %key; open my $data, '<', 'data.txt'; while(<$data>) { chomp; $key{$_} = []; } my %line; open my $all, '<', 'all.txt'; while(<$all>) { chomp; $line{key} = $_; $line{seq} = <$all>; # read the sequence chomp $line{seq}; my ($key) = ($line{key} =~ /(Contig\d*)/); if(exists $key{$key}) { push @{$key{$key}}, $line{seq}; } } print Dumper(\%key) ."\n";
Update: amended version per Re^2: compare a list from one file with another text file$VAR1 = { 'Contig25396' => [ 'GGGATCTTTGGACGAAGGGGGGAAAAAGATGTCAACTTTA +AGCATTCCAC CAATGCTTACTTCCCCTAGAGATGATGCCATTCAACTGTACAAGGCTTTC AAGGGAT +TTGGATGTGACACTTCTGCAGTAATCCATATCTTAGCTCGTCG' ], 'Contig25381' => [ 'GGACGAGATTTAACGACATCCATAAGCAACTCTGCTAATC +ATTCGATCTG CTTGGAGGTGTTTTTCCCCCATTTCCCTTAACCATGTCTCAGACTGTGGT' ], 'Contig25469' => [ 'GGCTCTCTACCTATCTGTCTCTCTCTACCTCTCTCTCCTT +TCACGCACAC' ] };
use strict; use warnings; use Data::Dumper; use Fatal qw[open]; my %key; open my $data, '<', 'data.txt'; while(<$data>) { chomp; $key{$_} = 1; } my %line; open my $all, '<', 'all.txt'; open my $out, '>', 'requery.txt'; while(<$all>) { chomp; $line{key} = $_; $line{seq} = <$all>; # read the sequence chomp $line{seq}; my ($key) = ($line{key} =~ /(Contig\d*)/); if(exists $key{$key}) { print $out join("\n", $line{key}, $line{seq}) . "\n"; } }
In reply to Re: compare a list from one file with another text file
by Arunbear
in thread compare a list from one file with another text file
by sm2004
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |