Using more recentish functionality of Text::CSV_XS, you get quite readable code IMHO:

use Text::CSV_XS "csv"; my $hr = csv ( in => "hr.txt", key => "samaccountname", keep_headers => \my @keys, ); my $aoh = csv (in => "ad.txt", bom => 1, on_in => sub { my $sam = $_[1]{samaccountname} or die "No name in AD"; my $ahr = $hr->{$sam}; unless ($ahr) { warn "I got AD data for $sam, not in HR\n"; next; } my @diff = map { [ $_, $ahr->{$_}, $_[1]{$_} ] } grep { $ahr->{$_} ne $_[1]{$_} } @keys; @diff or return; say "Changes for samaccount $sam"; printf " %-22s %-27.27s -> %s\n", @$_ for @diff; });

with the two datafiles you provided,

$ perl test.pl Changes for samaccount barsu991 mail Uttiam.Barski@pulse.org -> William.Barski +@pulse.org title Director of Cooks -> Chief of Cooks Changes for samaccount walkl003 givenname Lreblemet -> Larry employeenumber 20178941 -> mail Lreblemet.Walker@pulse.org -> Larry.Walker@p +ulse.org title Head Cook -> Cook Changes for samaccount karss001 givenname Sovyetk -> Steven mail Sovyetk.Karsten@pulse.org -> Steven.Karsten +@pulse.org title Dishwasher -> Dishw physicaldeliveryoffice Kitchen of the World -> Sully's Kitche +n streetaddress 205 Willy B. Temple -> 48720 Belcard st WI -> IL postalcode 50987 -> 34567 Changes for samaccount zingk072 givenname Kovon -> Kevin employeenumber 20113578 -> symphonyemployeetype IKP -> mail Kovon.Zingerman@pulse.org -> Kevin.Zingerma +n@pulse.org manager cn=manager1,ou=users,ou=Kit -> Changes for samaccount peizs194 givenname Synthia -> Samantha sn Smite -> Smith mail Synthia.Peizer@pulse.org -> Samantha.Smith +@pulse.org title Broiler Man -> Man, Broiler Changes for samaccount hutcy231 givenname Yello -> Yaren mail Yello Hutchinson -> Yaren Hutchins +on Changes for samaccount haserz221 sn Haserkrilk -> Hasermann employeenumber 20125471 -> mail Zebediah.Haserkrilk@kit.org -> Zebediah.Haser +man@pulse.org telephonenumber -> 555-555-5555

It is up to you to mold that into a report of your liking

Update: If you want to store the changes in a CSV file, change it like this:

my @diff; my $aoh = csv (in => "ad.txt", bom => 1, on_in => sub { my $sam = $_[1]{samaccountname} or die "No name in AD"; my $ahr = $hr->{$sam} or die "I got AD data for $sam, no +t in HR\n"; push @diff, map { [ $sam, $_, $ahr->{$_}, $_[1]{$_} ] } grep { $ahr->{$_} ne $_[1]{$_} } @keys; }); csv (in => \@diff, out => "diff.csv");

Enjoy, Have FUN! H.Merijn

In reply to Re: Comparing two files line by line and exporting the differences from the first file by Tux
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.