Operating System - HP-UX
1827283 Members
3447 Online
109717 Solutions
New Discussion

pulling email from file via script

 
SOLVED
Go to solution
Sean OB_1
Honored Contributor

pulling email from file via script

Does anyone have a quick and dirty script to pull out an email address from a file?

I have a file with about 3000 lines with email addresses at various places in the lines and I want to just pull out the email address and put them in a file.

The file looks like this:

; Tue, 9 Dec 2003 15:21:02 -0500
with SMTP id <0HPN00HGJ9OLLS@l-daemon> for Minden@shaw.ca; Tue,
(reason: 554 delivery error: dd This user doesn't have a swbell.net account (kar
iak@swbell.net) [-9] - mta825.mail.sc5.yahoo.com)
451 ... reply: read error from air-xj02.mail.aol.com.
5.1.0 - Unknown address error 550-'Invalid recipient: '
5.1.0 - Unknown address error 550-'Invalid recipient: '
550 5.0.0 ... Can't create output
550 5.0.0 ... Can't create output
550 5.0.0 ... Insufficient permission
550 5.1.1 <98c093@sirjamessmiths.cornwall.sch.uk>... User unknown
550 5.1.1 ... User unknown



I want to have just the email address from each line, and can deal with the <, (, ... etc. myself. So even if I ended up with

...

test@test.com

in the file that would be ok.

TIA,

Sean



16 REPLIES 16
Karthik S S
Honored Contributor

Re: pulling email from file via script

Hi Sean,

To filter only the lines that contains the e-mail address use,

grep '\>@\<' filename

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
RAC_1
Honored Contributor

Re: pulling email from file via script

grep -i "@" < input_file|tr -d" " "\n"|tr -d "<" "\n"|tr -d ">" "\n"|grep -i "@"

There is no substitute to HARDWORK
RAC_1
Honored Contributor

Re: pulling email from file via script

Slight correction

grep -i "@" < input_file|tr -d" " "\n"|tr -d "\<" "\n"|tr -d "\>" "\n"|grep -i "@"
There is no substitute to HARDWORK
Sean OB_1
Honored Contributor

Re: pulling email from file via script

RAC,

That doesn't work on my system:

# cat fg3 |tr -d" " "\n"
tr: illegal option --
Usage: tr [ -c | -cds | -cs | -ds | -s ] [-A] String1 String2
tr [ -cd | -cs | -d | -s ] [-A] String1


running 11i.

Sean OB_1
Honored Contributor

Re: pulling email from file via script

A couple of things to mention. The file already only has lines with @ in it, so no need to grep for that.

And the email addresses are not always deliminated by <>.

I was wondering if there was any way to simply key in on the @ and then grab everything up to the white space both before and after the @ sign.

Some lines may have more than one email address in them as well.

Karthik S S
Honored Contributor

Re: pulling email from file via script

Got it ....

grep '\>@\<' filename | awk -F"<" '{print $2}' | awk -F">" '{print $1}'

Will extract exactly the mail IDs.

Enjoy,
Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor
Solution

Re: pulling email from file via script

Sorry that I didn't read your post properly.

A real dirty one. But works !!!

for i in `cat filename`; do echo $i; done |grep @

The output is,

[root@lnxsrv tmp]# for i in `cat 123`; do echo $i; done |grep @
;
<0HPN00HGJ9OLLS@l-daemon>
Minden@shaw.ca;
iak@swbell.net)
...
'
'
...
...
...
<98c093@sirjamessmiths.cornwall.sch.uk>...
...
[root@lnxsrv tmp]#


However you will have to remove <, >, and ...s.

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: pulling email from file via script

In the above example the file "123" contained the following text (as in your first post)

-----------------------------------
; Tue, 9 Dec 2003 15:21:02 -0500
with SMTP id <0HPN00HGJ9OLLS@l-daemon> for Minden@shaw.ca; Tue,
(reason: 554 delivery error: dd This user doesn't have a swbell.net account (kar
iak@swbell.net) [-9] - mta825.mail.sc5.yahoo.com)
451 ... reply: read error from air-xj02.mail.aol.com.
5.1.0 - Unknown address error 550-'Invalid recipient: '
5.1.0 - Unknown address error 550-'Invalid recipient: '
550 5.0.0 ... Can't create output
550 5.0.0 ... Can't create output
550 5.0.0 ... Insufficient permission
550 5.1.1 <98c093@sirjamessmiths.cornwall.sch.uk>... User unknown
550 5.1.1 ... User unknown

-----------------------------------

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Michael Schulte zur Sur
Honored Contributor

Re: pulling email from file via script

Hi Sean,

try this:
while read LINE
do
EMAIL=`expr "${LINE}" : ".*<\([0-9,a-z,A-Z,.]*@[0-9,a-z,A-Z,.]*\)>.*"`
echo ${EMAIL}
done < file

greetings,

Michael

Karthik S S
Honored Contributor

Re: pulling email from file via script

Hi Michael,

That was a nice script but it seems to be missing out few e-mail IDs.

-------------------------------------------------------------------
[root@lnxsrv tmp]# while read LINE; do EMAIL=`expr "${LINE}" : ".*<\([0-9,a
-z,A-Z,.]*@[0-9,a-z,A-Z,.]*\)>.*"`; echo ${EMAIL}; done < file




jweirdeyes@wmconnect.com
ddphoto@sympatico.ca
iimiij@sympatico.ca
fhutsler@mbox1.greenapple.com
lisab@netrover.com
robbieduncan@optusnet.com.au
98c093@sirjamessmiths.cornwall.sch.uk
ASPROMATIS@RHO.FORTHNET.GR

-------------------------------------------------------------------

[root@lnxsrv tmp]# for i in `cat file`; do echo $i; done |grep @
arejayg@msys.net>;
<0HPN00HGJ9OLLS@l-daemon>
Minden@shaw.ca;
iak@swbell.net)
...
'
'
...
...
...
<98c093@sirjamessmiths.cornwall.sch.uk>...
...
[root@lnxsrv tmp]#
-------------------------------------------------------------------

For instance iak@swbell.net is not listed in the o/p of your script.

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
john korterman
Honored Contributor

Re: pulling email from file via script

Hi Sean,
how about this approach:

#!/usr/bin/sh
# carve out emailad
while read LINE
do
CUT_END=$(echo ${LINE#*@)
CUT_END=$(echo "$CUT_END"|awk '{print $1}')
CUT_BEGIN=$(echo ${LINE%%@*)
CUT_BEGIN=$(echo "$CUT_BEGIN"|awk '{print $NF}')
echo "${CUT_BEGIN}@${CUT_END}"
done <$1

with your input file as $1.

regards,
John K.
it would be nice if you always got a second chance
Mark Grant
Honored Contributor

Re: pulling email from file via script

Does this work?

perl -n -e 'print /.*\b(.+@[A-z,.]+)/;print "\n"' testfile
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: pulling email from file via script

Actually, this is probably a little better

perl -n -e 'print /.*\b([0-z]+@[0-z,.]+)/;print "\n"' testfile
Never preceed any demonstration with anything more predictive than "watch this"
Elmar P. Kolkman
Honored Contributor

Re: pulling email from file via script

As long as there is only 1 mail address on a line:

sed -n 's|^.*<\(.*@.*\..*\)>.*$|\1|p'
A mail address need to be enclosed in the brackets and needs at least 1 dot after the @
Every problem has at least one solution. Only some solutions are harder to find.
Michael Schulte zur Sur
Honored Contributor

Re: pulling email from file via script

Hi Karthik,

yes, you are right, I overlooked that emails could be enclosed by (.
This should fix it.

have fun,

Michael
Elmar P. Kolkman
Honored Contributor

Re: pulling email from file via script

Solution for mail addresses not enclosed in the brackets:
sed -n 's|^.* \([^ ]*@[^ ]*\.[^ ]*\) .*$|\1|p'

This only relies now on the mail address being enclosed by spaces... So brackets etc. will be printed too.
Every problem has at least one solution. Only some solutions are harder to find.