views:

124

answers:

7

How can I scan through a file which contains email addresses that are separated by a new line character and get rid of those that belong to a certain domain, e.g. [email protected]. I want to get rid of all email addresses that are @bad.com

+8  A: 

Use grep instead of Perl

grep -v '@bad\.com' inputfile > outputfile

On Windows

findstr /v "@bad\.com" inputfile > outputfile
Jim Garrison
+1 nice n clean. Thanks for the windows answer too.
Byron Whitlock
you should escape the dot.
ghostdog74
@ghostdog74: good point; done
Jim Garrison
what about "[email protected].*"? Are also to be filtered out?
Leonardo Herrera
A: 

This should do:

$badDomain = "bad.com";
while(<>)
{
        s{\s+$}{};
        print "$_\n" if(!/\@$badDomain$/);
}
codaddict
Since we never `chomp()`-ed the line, it will already have a newline at the end by default. You don't need to print it with another one (unless of course you want blank lines between your output lines).
Chris Lutz
@Chris: If you look closely at line 4, I'm removing all trailing whitespaces. That will remove the trailing \n as well. So a \n in the print is needed.
codaddict
Ah. In that case, why not `s/\s+$/\n/;` so the newline is kept, then just `print if /regex/` ?
Chris Lutz
A: 

this code should filter all the @bad.com address from the input files.

 my @array = <>;

 foreach(@array) {
   if(!/\@bad.com$/) {
     print $_;
   }
 }
dan
That's awful. Why would you slurp in `<>` when you could just iterate over it for the same effect, with almost no memory impact?
Chris Lutz
A: 

Perl

perl -ne 'print if !/@bad\.com/' file

awk

awk '!/@bad\.com/' file 
ghostdog74
That's not the right pattern. It also excludes notbad.com, etc.
brian d foy
A: 

The following would allow you to have a script that you can enhance in time... Instead of simply filtering out @bad.com (which you can do with a simple grep), you can write your script so you can easily sophisticate which domains are unwanted.

my $bad_addresses = {'bad.com'=>1};

while (my $s = <>) {
    print $s unless (is_bad_address($s));
}

sub is_bad_address {
    my ($addr) = @_;
    if ($addr=~/^([^@]+)\@([^@\n\r]+)$/o) {
        my $domain = lc($2);
        return 0 unless (defined $bad_addresses->{$domain});
        return $bad_addresses->{$domain};
    }
    return 1;
}
Zoran Simic
A: 

Email::Address is a nice module for dealing with email addresses.

Here is an example which may whet you appetite:

use Email::Address;

my $data = 'this person email is [email protected]
blah blah [email protected] blah blah
[email protected]
';

my @emails      = Email::Address->parse( $data );
my @good_emails = grep { $_->host ne 'bad.com' } @emails;

say "@emails";       # => [email protected] [email protected] [email protected]
say "@good_emails";  # => [email protected]

/I3az/

draegtun
A: 

Not too different of what others have done.

use strict;
use warnings;

my @re = map { qr/@(.*\.)*\Q$_\E$/ } qw(bad.com mean.com);

while (my $line = <DATA>) {
    chomp $line;
    if (grep { $line =~ /$_/ } @re) {
        print "Rejected: $line\n";
    } else {
        print "Allowed: $line\n";
    }
}

__DATA__
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
Leonardo Herrera