Given these two CSV-files:
file1
and file2key1,key2,data1,data2,data3 A,1,A1d1,A1d2,A1d3 A,2,A2d1,A2d2,A2d3 B,1,B1d1,B1d2,B1d3
The following program combines the matching lines from both files: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
Output: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();
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |