in reply to Re: Reconcile one list against another
in thread Reconcile one list against another
#!/usr/bin/perl use strict; use Data::Dumper; # Grep a user OUT of a file, given a list of names, likely from anothe +r file # psuedo code: # if file1 contains a name from file2, skip the line # if file1 does NOT contain a name from file2, print the line. # # I am using this to determine a list of accounts on the partners serv +er # that, *maybe*, shouldn't be on there any more. my @users; my %lines; my $file1=$ARGV[0]; my $file2=$ARGV[1]; open FILE1, "<$file1" or die "Cannot open $file1: $!\n"; # Hasherize it!!! foreach my $line (<FILE1>) { chomp($line); $lines{$line}=0; } close FILE1; # Create @users array open FILE2, "<$file2" or die "Cannot open $file2: $!\n"; foreach my $line (<FILE2>) { if ($line =~ /#/) { my @out=split /\s+/,$line; push @users,$out[1]; } } close FILE2; foreach my $key (keys(%lines)) { foreach my $user (@users) { if ($key =~ /$user/i) { $lines{$key}++; } } } print "List of users not authorized to have an account\n"; foreach my $key (sort keys(%lines)) { if ($lines{$key} <= 0) { print "$key\n"; } } print "\nList of users authorized to have an account\n"; foreach my $key (sort keys(%lines)) { if ($lines{$key} > 0) { print "$key\n"; } }
It handily prints out what should be there, and what should not. Now all I have to do is some manual verification, and I can call this a lesson learned.
Many thanks to all those with quick replies, and mostly to jpl for the hint in the direction of hashes.
wind: You code is excellent, but only prints out those folks authorized to have accounts, how would I modify your code to print the inverse list? I tried negating the match (=~ to !=~), but it failed in a quite spectacular way.
UPDATE:
I added a sort into each foreach loop above to print out the lines in alphabetical order.
Very funny Scotty... Now PLEASE beam down my PANTS!
|
|---|