We help IT Professionals succeed at work.

perl - compare list

bt707
bt707 asked
on
Medium Priority
868 Views
Last Modified: 2012-05-06
I have two files that both have a list of email addresses in them

1) org-list.txt
2) new-list.txt

I need to find what address is in the new-list.txt that is not in the org-list.txt

I can do that with this code I use:


#!/usr/bin/perl
use strict;
my($file1,$file2);

my %file1;
open (FILE1, "<org-list.txt");
while(<FILE1>) {
 $file1{lc $_}=1;
}
close(FILE1);

open (FILE2, "<new-list.txt");
while (<FILE2>) {
  unless ($file1{lc $_}) {
    print $_;
  }
}


=====================================

I have the org-list.txt already made and I can make the new-list.txt using this code I use:


!/usr/bin/perl
use strict;
open(FH,"</mail/logs/users.txt");
while (<FH>){
$_=lc;
chomp;
        my ($domain) = (/From:.*?<.*slb.com>/i);
        my ($from) = (/X-Mailer: .*Message-ID:.*<.*abc.com> From:.*<(.*?)>/i);
        my ($outlook) = (/X-Mailer: .*(Outlook)/i);
        if($domain and $outlook and $from) {
          print "$from\n";
        }
}


=====================================


I can create the new list as show above then run the first code to get what I want, but where I'm stuck now is putting this together, not just sure how to do this.

I need to edit the second code so it will get the new list of address, sort unique them, put that list in a % then compare each mail address with the list that is in the org-list.txt, if any address is found on the new-list that is NOT in the org-list.txt then print that out.

Hope this explanation came out ok.


I have want I want working, I'm just looking to learn a bit more and combine the two scripts I'm using so I can run the second code above that gets my new list then compares with the org-list.txt so I can do this in one step instead of two.


Thanks for any help.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
#!/usr/bin/perl
use strict;

my %file1;
open (FILE1, "<org-list.txt");
while(<FILE1>) {
 chomp;
 $file1{lc $_}=1;
}
close(FILE1);

my %file2;
open(FH,"</mail/logs/users.txt");
while (<FH>){
$_=lc;
        my ($domain) = (/From:.*?<.*slb.com>/i);
        my ($from) = (/X-Mailer: .*Message-ID:.*<.*abc.com> From:.*<(.*?)>/i);
        my ($outlook) = (/X-Mailer: .*(Outlook)/i);
        if($domain and $outlook and $from and !$file1{$from} ) {
           $file2{$from}++
        }
}
close FH;

print "$_\n" for sort keys %file2;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
ozo,

Worked perfect.

Thank you very much!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.