As I am in a good mood, I have cobbled together a quick example.

Given these two CSV-files:

file1

key1,key2,data1,data2,data3 A,1,A1d1,A1d2,A1d3 A,2,A2d1,A2d2,A2d3 B,1,B1d1,B1d2,B1d3
and file2
key1,key2,data4,data5 A,1,A1d4,A1d5 A,2,A2d4,A2d5 B,1,B1d4,B1d5 A,2,A2d4bis,A2d5bis A,2,A2d4ter,A2d5ter B,1,B1d4bis,B1d5bis
The following program combines the matching lines from both files:
use strict; use warnings; use 5.012; use DBD::CSV; my $dbh = DBI->connect( "dbi:CSV:f_dir=.", undef, undef, { FetchHashKeyName => "NAME_lc", RaiseError => 1, PrintError => 1, + } ) or die $DBI::errstr; my $query = 'SELECT key1, key2, data1, data2, data3, data4, data5 FROM file1 JOIN +file2 WHERE file1.key1 = file2.key1 AND file1.key2 = file2.key2'; my $sth = $dbh->prepare($query); $sth->execute(); while ( my @row = $sth->fetchrow_array ) { say join '|', @row; } $sth->finish();
Output:
A|1|A1d1|A1d2|A1d3|A1d4|A1d5 A|2|A2d1|A2d2|A2d3|A2d4|A2d5 A|2|A2d1|A2d2|A2d3|A2d4bis|A2d5bis A|2|A2d1|A2d2|A2d3|A2d4ter|A2d5ter B|1|B1d1|B1d2|B1d3|B1d4|B1d5 B|1|B1d1|B1d2|B1d3|B1d4bis|B1d5bis

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: Compare Two Text Files Based on a String by CountZero
in thread Compare Two Text Files Based on a String by Anonymous Monk

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.