CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Extract email from txt file

 



mrsaqib
Novice

Oct 12, 2009, 11:24 AM

Post #1 of 2 (3074 views)
Extract email from txt file Can't Post

hi there

i have a requirement, i need to extract sender' e-mail address (From:) from following file,

File 'file.txt' is as under:
====================

From root@localhost.localdomain Fri Oct 9 06:02:09 2009
Return-Path: <root@localhost.localdomain>
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by localhost.localdomain (8.13.8/8.13.8) with ESMTP id n991283v009188
for <saqib@localhost>; Fri, 9 Oct 2009 06:02:08 +0500
Message-Id: <200910090102.n991283v009188@localhost.localdomain>
MIME-Version: 1.0
Content-Transfer-Encoding: binary
Content-Type: multipart/mixed; boundary="_----------=_125505012891870"
X-Mailer: MIME::Lite 3.025 (F2.74; T1.28; A2.03; B3.07; Q3.07)
Date: Fri, 9 Oct 2009 06:02:08 +0500
From: root@localhost.localdomain
To: saqib@localhost.localdomain
Subject: This is the message subject
Status: O

This is a multi-part message in MIME format.

--_----------=_125505012891870
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
Content-Type: text/plain

This is the message body
--_----------=_125505012891870
Content-Disposition: attachment; filename="mario.jpg"
Content-Transfer-Encoding: base64
Content-Type: image/jpeg; name="mario.jpg"
................................................................................................................... so on



* Note : i need only from address i.e, root@localhost.localdomain

Can someone kindly help me to extract the 'From' email address.
I will be really thankful to you.
Attachments: file.txt (44.1 KB)


ichi
User

Oct 12, 2009, 6:57 PM

Post #2 of 2 (3067 views)
Re: [mrsaqib] Extract email from txt file [In reply to] Can't Post

what have you tried?

pseudocode:


Code
while  ( looping through file ){ 
check to see if line starts with "From" # hint: use regex ^ , or use index() ; see perldoc -f index
if ( it starts with "From" ) {
split the line on space # hint : perldoc -f split
get the second element of the array output by split()
print it out
exit
}

}


in short if you want to program in Perl, all you have to do is RTFM:
1) perldoc -f split
2) perldoc -f index
3) perldoc perlre


(This post was edited by ichi on Oct 12, 2009, 6:58 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives