open(OUT1, ">> resultsfile.csv")||die("Failed to open resultsfile.csv: $!\n"); open(IR, "< file1.csv")|| die("failed to open file1"); open(BS, "< file2.csv")||die ("failed to open file2"); while (defined ($line=)) { chomp $line; ($username, $firstname, $lastname, $lastlogin, $tokenexpiration) = split(",", $line); while (defined ($line2=)) { if ($line2 =~ /$lastname/ && $line2 =~ /$firstname/) { ($bs, undef) = split(",", $line2, 2); print OUT1 "$bs $username,$firstname,$lastname,$lastlogin,$tokenexpiration\n"; last; # go to next name in outer loop. } } } #### __Data__ From file 1 Alan,Bloggs,06/11/2009,11/30/2011 David,Smith,06/08/2009,09/30/2012 Rosario,Anotherone,06/05/2009,11/30/2011 Angela,Madeupname,06/11/2009,07/31/2010 Ugochukwu,Smith,06/01/2009,10/31/2012 Amarjit,Patel,08/19/2008,11/30/2011 Julie,Schmidt,05/01/2009,09/30/2012 Waseem,Alder,06/11/2009,11/30/2011 ... for another 580 lines. File 2 abc,Alan,Bloggs,06/11/2009,11/30/2011,morefields,... cde,David,Smith,06/08/2009,09/30/2012,morefields,... abc,Rosario,Anotherone,06/05/2009,11/30/2011,morefields,... acd,Angela,Madeupname,06/11/2009,07/31/2010,morefields,... cde,Ugochukwu,Smith,06/01/2009,10/31/2012,morefields,... tla,Julie,Schmidt,05/01/2009,09/30/2012,morefields,... tl,Waseem,Alder,06/11/2009,11/30/2011,morefields,... __End of Data__ #### !/usr/bin/perl -w use Time::Local; use strict; use warnings; use diagnostics; use Data::Dumper; # declare stuff to avoid errors my (%user_bs $user_bs @temp, @temp2, @user_expiration_inf, $firstname, $current, $username, $lastname, $lastlogin, $tokenexpiration, $month, $day, $year, $edate, $line, $line2, $bs, $b1, $y, $z, $name, $name2); our $/; $current = time(); # slurp up the first file into one huge hash ( name => value ) open(BSFILE, "< file1.csv") || die("Can't open file1.csv: $!\n"); while (defined ($line=)) { next if $line =~ /^Data here/; chomp $line; ($b1, $y, $z) = split(/,/, $line); $name = "$z $y"; $user_bs{ $name } = $b1; } close(BSFILE); # Now to whack file two into an array of arrays. open(USERLIST, $ARGV[0]) || die("Can't open $ARGV[0]: $!\n"); while (<>) { next if /^Default Login/; # ignore first line ($username, $lastname, $firstname, $lastlogin, $tokenexpiration) = split(",", $_))[0, 1, 2, 5, 8]; ($month, $day, $year) = split('/', $tokenexpiration); $edate = timelocal(00,00,00,$day,$month-1,$year-1900); next if $edate < $current; # remove from list all users whose tokens expired before today $name2 = "$firstname $lastname"; @temp2 = ($username, $name2, $lastlogin, $tokenexpiration); push @user_expiration_inf, [@temp2]; } close(USERLIST); open(OUT, ">>results.csv") || die("Can't open results.csv: $!\n"); print OUT "User, Data Here, Username, Last login, Token expiration date\n"; # nice while loop to iterate through the array of arrays my $i = 0; while ($user_expiration_inf[$i][1]) { print OUT "$user_expiration_inf[$i][1], $user_bs{$user_expiration_inf[$i][1]}, $user_expiration_inf[$i][0], $user_expiration_inf[$i][2], $user_expiration_inf[$i][3]\n"; $i++; }