in reply to Comparing two files line by line and exporting the differences from the first file

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
  • Comment on Re: Comparing two files line by line and exporting the differences from the first file
  • Select or Download Code