새 줄 문자로 구분 된 이메일 주소가 포함 된 파일을 스캔하고 특정 도메인에 속한 이메일 주소를 제거하는 방법은 무엇입니까? [email protected]
.Perl을 사용하여 특정 도메인에 속한 이메일 주소를 필터링하는 방법은 무엇입니까?
$badDomain = "bad.com";
while(<>)
{
s{\s+$}{};
print "$_\n" if(!/\@$badDomain$/);
}
새 줄 문자로 구분 된 이메일 주소가 포함 된 파일을 스캔하고 특정 도메인에 속한 이메일 주소를 제거하는 방법은 무엇입니까? [email protected]
.Perl을 사용하여 특정 도메인에 속한 이메일 주소를 필터링하는 방법은 무엇입니까?
$badDomain = "bad.com";
while(<>)
{
s{\s+$}{};
print "$_\n" if(!/\@$badDomain$/);
}
사용 grep
대신 펄
grep -v '@bad\.com' inputfile > outputfile
findstr /v "@bad\.com" inputfile > outputfile
모든 이메일 주소를 제거하려면 입력 파일에서 @ bad.com 주소를 모두 필터링해야합니다.
my @array = <>;
foreach(@array) {
if(!/\@bad.com$/) {
print $_;
}
}
우리는'chomp()'하지 않았으므로 기본적으로 끝에 줄 바꿈이 이미 있습니다. 물론 출력 라인 사이에 빈 줄을 쓰지 않는 한, 다른 것으로 인쇄 할 필요는 없습니다. –
@Chris : 4 번 라인을 자세히 살펴보면, 뒤 따르는 모든 공백을 제거하고 있습니다. 그러면 후행 \ n도 제거됩니다. 그래서 인쇄물에 \ n이 필요합니다. – codaddict
아. 이 경우,'s/\ s + $/\ n /;'이 아닌 줄 감은 줄 바꿈이 생기면'print if/regex /'? –
이 코드 :이 무엇을해야 @bad.com
그건 끔찍한 일입니다. 왜 메모리 효과가 거의없이 같은 효과를 내기 위해 반복 할 수있을 때'<>'를 사용하면 어떨까요? –
펄
perl -ne 'print if !/@bad\.com/' file
AWK
awk '!/@bad\.com/' file
그건 맞는 패턴이 아니야. notbad.com 등도 제외됩니다. –
다음은 당신이 시간을 향상시킬 수있는 스크립트를 가질 수 있도록 할 다음 ... 대신 단순히 필터링 @ 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;
}
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]
너무 다른되지 않음 : 여기
당신에게 식욕을 자극 수있는 예이다.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]
+1 nice n clean. windows 답변 주셔서 감사합니다. –
점을 벗어나야합니다. – ghostdog74
@ ghostdog74 : 좋은 지적; done –