LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2013, 11:24 AM   #1
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Rep: Reputation: Disabled
Question Perl Script to extract email


Having a little trouble with the below perl script

Code:
#!/usr/bin/perl
#Author Leo
use Email::Address;

#use strict;
my $file = "/var/log/maillog";
my $string="75E37A371C";
open(MAIL, $file);
my @buffer =<MAIL>;
close(MAIL);
my $lines=grep(/$string/, @buffer);

#print "@lines";

my @addresses = Email::Address->parse($lines);
print $addresses[0]->address;
Not sure how to print the output, tried line as an array as well
 
Old 05-16-2013, 12:16 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
You want lines to be an array, not a scalar:

Code:
my @lines=grep(/$string/, @buffer);

my @addresses = Email::Address->parse(@lines);
foreach (@addresses) {
   print
}
 
Old 05-17-2013, 02:35 AM   #3
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
Tried that as well but still no luck

Code:
#!/usr/bin/perl
#Author Leo
use Email::Address;

#use strict;
my $file = "/var/log/maillog";
my $string="75E37A371C";
open(MAIL, $file);
my @buffer =<MAIL>;
close(MAIL);
my @lines=grep(/$string/, @buffer);

print "@lines";

my @addresses = Email::Address->parse(@lines);
#print "@addresses";
foreach (@addresses) {
print "$addresses[0]->address";
}
Not sure if the print is correct though
 
Old 05-17-2013, 06:14 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The grep should work, but Email::Address->parse works on strings, not arrays
Quote:
This class implements a regex-based RFC 2822 parser that locates email addresses in strings
http://search.cpan.org/~rjbs/Email-A...ail/Address.pm

Note that the example on that page
Code:
Class Methods

parse

      my @addrs = Email::Address->parse(
        q[me@local, Casey <me@local>, "Casey" <me@local> (West)]
      );
Looks a bit like an array (uses [ ] chars), but this is actually an example of quoting
Quote:
with q, qq, and qw, delimiters other than parentheses can be used. [] and {} will work, as will just about any punctuation mark,
http://www.perlmonks.org/?node_id=401006

Incidentally, I always start Perl with 'warnings' and 'strict' turned on; highly recommend you do the same.
You'll thank me later
Code:
#!/usr/bin/perl -w

use strict;
 
Old 05-17-2013, 07:49 AM   #5
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thank you chrism01, I will use strict and warnings from now on.

Can you tell me How I can extract the email id's only,

I wanna feed them to a database and I am actually contemplating using shell instead.
 
Old 05-17-2013, 08:23 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Basically, you need to loop through the @lines array, 1 element at a time and parse that (or indeed loop through @buffer).
In your code you've already specified the Id to match on, so I don't understand your qn.

Do you want addresses or Ids?
 
Old 05-17-2013, 08:40 AM   #7
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
Hi,

It will print out a line as below

Code:
May 16 21:00:53 mspwss sendmail[32248]: 75E37A371C: to=leo@leog.in, ctladdr= (664/664)

I want to strip the email id only from this line, I am gathering data on 550 errors and I wanna check which email ids are frequently used so I can block them
 
Old 05-20-2013, 02:17 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
$var1='May 16 21:00:53 mspwss sendmail[32248]: 75E37A371C: to=leo@leog.in, ctladdr= (664/664)';
$var2 = (split(/:/, $var1))[3];
print "var2 $var2\n";

#output
var2  75E37A371C


HTH
 
Old 05-20-2013, 02:28 AM   #9
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Code:
$var1='May 16 21:00:53 mspwss sendmail[32248]: 75E37A371C: to=leo@leog.in, ctladdr= (664/664)';
$var2 = (split(/:/, $var1))[3];
print "var2 $var2\n";

#output
var2  75E37A371C


HTH
That Gives the message id, I want the email id
 
Old 05-20-2013, 02:37 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You mean 32248 ?
Code:
$var1='May 16 21:00:53 mspwss sendmail[32248]: 75E37A371C: to=leo@leog.in, ctladdr= (664/664)';
$var1 =~ /\[([[:digit:]]+)\]/;
print "$1\n";

# out
32248
 
Old 05-20-2013, 03:04 AM   #11
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
No the output should be

leo@leog.in
 
Old 05-20-2013, 04:53 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Ah, the email address. The other 2 nums are email Id and the msg Id.
Code:
$var1='May 16 21:00:53 mspwss sendmail[32248]: 75E37A371C: to=leo@leog.in, ctladdr= (664/664)';
$var2 = (split(/\s+/,$var1))[6];
$var2 =~ /=([a-z]+@.*),/;
print "$1\n";

#out
leo@leog.in
 
1 members found this post helpful.
Old 05-20-2013, 09:26 AM   #13
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thank you Chris,

I seem to be getting the below error though any idea why

Use of uninitialized value $1 in concatenation (.) or string at ./email.pl line 24.


Code

Code:
#!/usr/bin/perl
#Author Leo
use strict;
use warnings;


my $file = "maillog";
my $string="550";
open(MAIL, $file);
my @buffer =<MAIL>;
close(MAIL);
my @lines=grep(/$string/, @buffer);

#print "@lines";

foreach my $line (@lines){
#print $line


my $email = (split(/\s+/,$line))[6];
$email =~ /=([a-z]+@.*),/;

print "$1\n";

}
 
Old 05-21-2013, 12:45 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You need to check exactly what you're getting in each element of each of those arrays.
Try printing them to a file and have a good look; you need to know exactly what data you're dealing with before you can craft algorithms/regexes to deal with them.

If you get that error, it means there wasn't a matching email addr in that string.
 
Old 05-21-2013, 01:49 AM   #15
Leo-G
Member
 
Registered: May 2013
Distribution: Cent OS 6
Posts: 32

Original Poster
Rep: Reputation: Disabled
Thank you Chris, I will check further
 
  


Reply

Tags
perlscript



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipe email to extract attachments. Script works, but email sender receives an error! ceashton Linux - Server 4 01-08-2019 11:13 AM
perl script to extract string ge1 Linux - Software 4 07-07-2011 12:14 PM
script/application to extract attachments from email Murdock1979 Linux - Server 1 05-08-2008 08:36 AM
awk/perl script for extract data mruknown1 Programming 3 09-11-2007 09:19 AM
perl extract email subject winchester169 Linux - General 1 08-24-2006 04:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration