This lets you compare two csv files and choose which columns should be unique. The code isn't realy pretty but it was a quick hack and i though others might find it usefull. If nothing else now i'll be able to find ti agian if i loose it. ;)

#!/usr/bin/perl use strict; use warnings; use Text::CSV; use Data::Dumper; my ($key, $file1, $file2) = (shift, shift,shift); die "Usage: compare <keycol> <file1> <file2>\n" unless defined $file1 && defined $file2 && defined $key; open( my $fh1, "<", $file1) or die "Failed to open '$file1'"; open( my $fh2, "<", $file2) or die "Failed to open '$file2'"; my $csv = Text::CSV->new; my @key_cols = split ",", $key; my @lines1 = <$fh1>; my @lines2 = <$fh2>; chomp @lines1; chomp @lines2; my ($hash1, $hash2, $hash_all); for my $line (@lines1) { if ($csv->parse($line)) { my @field = $csv->fields; $key = "";$key .= "__" . uc($field[$_]) for @key_cols; $hash1->{$key} = @field; $hash_all->{$key} = $line; } } for my $line (@lines2) { if ($csv->parse($line)) { my @field = $csv->fields; $key = "";$key .= "__" . uc($field[$_]) for @key_cols; $hash2->{$key} = @field; $hash_all->{$key} = $line; } } for my $key (keys %$hash_all) { print "1,", $hash_all->{$key},"\n" unless exists $hash1->{$key}; print "2,", $hash_all->{$key},"\n" unless exists $hash2->{$key}; }

___________
Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;

In reply to Compare 2 csv files using a key set of colums by eric256

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.