Here you go:

#!/usr/local/bin/perl use strict; use warnings; # NOTE: This program loads all of file1 and file2 into memory, # so if they're huge, this might not work. my $infile1 = "file1.txt"; my $infile2 = "file2.txt"; #-------------------------------------------------------------------- my %f2 = (); # KEY=col1, VAL=col2 (in file2) open my $INFILE2, '<', $infile2 || die "Cannot open \"$infile2\"!\n"; LINE: while(my $line=<$INFILE2>) { chomp $line; next LINE if($line eq ""); # Skip empties my @input = split(/,/, $line); $f2{ $input[0] } = $input[1]; # "d" => 90 } #LINE close $INFILE2; #-------------------------------------------------------------------- my %f1 = (); open my $INFILE1, '<', $infile1 || die "Cannot open \"$infile1\"!\n"; LINE: while(my $line=<$INFILE1>) { chomp $line; next LINE if($line eq ""); # Skip empties # Col3=$2, Col5=$3 if($line =~ m/(\S+, \S+, \S+, (\S+), \S+, (\S+))/) { $f1{$2}->{$3} = $1; } else { next LINE; } } #LINE close $INFILE1; #-------------------------------------------------------------------- my $outfilename = "output.txt"; open my $OUTFILE, '>', $outfilename; FILE2COL: foreach my $col1_f2 (keys %f2) { next FILE2COL unless (exists $f1{$col1_f2}); while(my($col5_f1, $line_f1) = each %{ $f1{$col1_f2} }) { my $to_match = $f2{$col1_f2}; print $OUTFILE $line_f1."\n" if( $line_f1 =~ m/$to_match/); } } close $OUTFILE;

In reply to Re: CSV Pattern Matching by traceyfreitas
in thread CSV Pattern Matching by PerlNewbRP

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.