Hello monks, here is my question. I have 2 files, one containing a list of IDs that looks like this:

GSAD1234 GSAD2345 GSAD4567
And another one that looks like this, it is a csv with tab as field delimiter (which I symbolize here with " \t " as somehow I can't add a real tab here, also please note that I did not forget a tab in "no match", there are really fields that do contain white spaces):
GSAD1234 \t 123 \t 45 \t no match \t fungus \t protein_x GSAD5678 \t 123 \t 51 \t plant \t fungus \t protein_y \t transporter
It is worth mentioning this second file contains more than 50 000 lines.

I would like to extract from the second file the lines corresponding to the IDs from the first file. So here the desired output would be:

GSAD1234 \t 123 \t 45 \t no match \t fungus \t protein_x
How do I do that? I had thought of reading the second file into a hash with the IDs as key and the rest of the fields as values but I can't find a way to do it due to the multiple fields per line. So far I have read the 2 files into arrays and tried to match the lines but it doesn't work and again, I am not sure it is the right strategy. Here is the code that seems to simply output the whole csv file:
#!/usr/bin/perl use warnings; use strict; use Text::CSV; use File::Slurp; my $csv = Text::CSV->new({ sep_char => '\t' }); #end of preparation #read data my $file = $ARGV[0] or die "Need to get CSV file on the command line\n +"; open(my $data,'<',$file) or die "Could not open file \n"; chomp (my @strings = <$data>); close $data; # read ID list my $id = 'id.txt'; my @ids = read_file("$id", chomp =>1); foreach(@ids) { my @matches = grep(/^($_)/,@strings); print join ",",@matches; }
I would be grateful for any help.


In reply to Extracting lines starting with a pattern from an array by Alessandro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.