G'day jzelkowsz,

Here's a solution using Text::CSV (if you have Text::CSV_XS installed it will run faster) and in-memory files (see open). The input data I used is a verbatim copy of what you posted here.

#!/usr/bin/env perl use strict; use warnings; use Text::CSV; my ($hr_file, $ad_file, $com_file) = qw{hr.txt ad.txt com.txt}; my (@col_index, %hr_record_for); my $csv = Text::CSV::->new({quote_space => 0}) or die "Can't instantiate a Text::CSV object: ", Text::CSV::->error_diag(); { open my $mem_fh, '<', canonicalise_file_in_memory($hr_file) or die "Can't read in-memory file: $!"; @col_index = @{$csv->getline($mem_fh)}; while (my $row = $csv->getline($mem_fh)) { $hr_record_for{$row->[0]} = $row; } } { open my $mem_fh, '<', canonicalise_file_in_memory($ad_file) or die "Can't read in-memory file: $!"; open my $out_fh, '>', $com_file or die "Can't write '$com_file': $!"; (undef) = $csv->getline($mem_fh); while (my $row = $csv->getline($mem_fh)) { for my $i (1 .. $#col_index) { if ($hr_record_for{$row->[0]}[$i] ne $row->[$i]) { $csv->say($out_fh, [ $row->[0], $col_index[$i], $hr_record_for{$row->[0]}[$i] ]); } } } } sub canonicalise_file_in_memory { my ($file) = @_; open my $fh, '<', $file or die "Can't read '$file': $!"; my $canon; while (<$fh>) { chomp if /,$/; $canon .= $_; } return \$canon; }

Output:

$ cat com.txt barsu991,mail,Uttiam.Barski@pulse.org barsu991,title,Director of Cooks walkl003,givenname,Lreblemet walkl003,employeenumber,20178941 walkl003,mail,Lreblemet.Walker@pulse.org walkl003,title,Head Cook karss001,givenname,Sovyetk karss001,mail,Sovyetk.Karsten@pulse.org karss001,title,Dishwasher karss001,physicaldeliveryoffice,Kitchen of the World karss001,streetaddress,205 Willy B. Temple karss001,st,WI karss001,postalcode,50987 zingk072,givenname,Kovon zingk072,employeenumber,20113578 zingk072,symphonyemployeetype,IKP zingk072,mail,Kovon.Zingerman@pulse.org zingk072,manager,"cn=manager1,ou=users,ou=Kitchen,dc=Kitchen,dc=net" hutcy231,givenname,Yello hutcy231,mail,Yello Hutchinson haserz221,sn,Haserkrilk haserz221,employeenumber,20125471 haserz221,mail,Zebediah.Haserkrilk@kit.org haserz221,telephonenumber,

— Ken


In reply to Re: Comparing two files line by line and exporting the differences from the first file by kcott
in thread Comparing two files line by line and exporting the differences from the first file by jzelkowsz

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.