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}; }
In reply to Compare 2 csv files using a key set of colums by eric256
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |