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
views:
124answers:
7
+8
A:
Use grep
instead of Perl
grep -v '@bad\.com' inputfile > outputfile
On Windows
findstr /v "@bad\.com" inputfile > outputfile
Jim Garrison
2009-12-28 20:27:29
+1 nice n clean. Thanks for the windows answer too.
Byron Whitlock
2009-12-28 21:07:50
you should escape the dot.
ghostdog74
2009-12-28 23:50:20
@ghostdog74: good point; done
Jim Garrison
2009-12-29 04:31:54
what about "[email protected].*"? Are also to be filtered out?
Leonardo Herrera
2009-12-29 15:53:44
A:
This should do:
$badDomain = "bad.com";
while(<>)
{
s{\s+$}{};
print "$_\n" if(!/\@$badDomain$/);
}
codaddict
2009-12-28 20:41:59
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
2009-12-28 21:21:31
@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
2009-12-29 02:46:50
Ah. In that case, why not `s/\s+$/\n/;` so the newline is kept, then just `print if /regex/` ?
Chris Lutz
2009-12-29 09:46:17
A:
this code should filter all the @bad.com address from the input files.
my @array = <>;
foreach(@array) {
if(!/\@bad.com$/) {
print $_;
}
}
dan
2009-12-28 21:04:04
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
2009-12-28 21:17:30
A:
Perl
perl -ne 'print if !/@bad\.com/' file
awk
awk '!/@bad\.com/' file
ghostdog74
2009-12-28 23:48:07
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
2009-12-29 00:49:03
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
2009-12-29 15:03:39
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
2009-12-29 16:02:27