如何使用 Perl 过滤属于特定域的电子邮件地址?

发布于 2024-08-16 18:25:40 字数 213 浏览 7 评论 0原文

如何扫描包含由换行符分隔的电子邮件地址的文件,并删除属于特定域的地址,例如 [电子邮件受保护]。我想删除所有 @bad.com 电子邮件地址

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

流星番茄 2024-08-23 18:25:40

使用 grep 代替 Perl

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

在 Windows 上

findstr /v "@bad\.com" inputfile > outputfile

Use grep instead of Perl

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

On Windows

findstr /v "@bad\.com" inputfile > outputfile
一笑百媚生 2024-08-23 18:25:40

Email::Address 是一个很好的处理模块与电子邮件地址。

下面这个例子可能会引起你的兴趣:

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]

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]
长安忆 2024-08-23 18:25:40

这应该做:

$badDomain = "bad.com";
while(<>)
{
        s{\s+$}{};
        print "$_\n" if(!/\@$badDomain$/);
}

This should do:

$badDomain = "bad.com";
while(<>)
{
        s{\s+$}{};
        print "$_\n" if(!/\@$badDomain$/);
}
椵侞 2024-08-23 18:25:40

以下内容将允许您拥有一个可以及时增强的脚本...而不是简单地过滤掉@bad.com(您可以使用简单的 grep 来完成),您可以编写脚本,以便您可以轻松地复杂化哪些域是不需要的。

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;
}

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;
}
一花一树开 2024-08-23 18:25:40

与其他人所做的并没有太大不同。

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]

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]
淡写薰衣草的香 2024-08-23 18:25:40

Perl

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

awk

awk '!/@bad\.com/' file 

Perl

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

awk

awk '!/@bad\.com/' file 
爱的故事 2024-08-23 18:25:40

这段代码应该从输入文件中过滤掉所有@bad.com 地址。

 my @array = <>;

 foreach(@array) {
   if(!/\@bad.com$/) {
     print $_;
   }
 }

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

 my @array = <>;

 foreach(@array) {
   if(!/\@bad.com$/) {
     print $_;
   }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文