On Tue, Nov 29, 2011 at 4:13 PM, Rajeev Prasad <[email protected]> wrote:

> hello,
>
> i am trying to extract email address from a afile, but not quite
> succesful. here is what i have:
>
> the file:
> myterqlqt qntmrq Prqtesm qltul qzeez Smqik [email protected] 976665
> myterqlqt qntmrq Prqtesm teepqk Mittql [email protected] 939383
> Onjole qntmrq Prqtesm lmqrqtm Etqrq [email protected] 9889
> Vijqyqwqtq qntmrq Prqtesm Sitmqrtmq [email protected] 939775777
> Visqkmqpqtnqm qntmrq Prqtesm Smyqmprqsqt Mqntri 
> [email protected]
> Wqrqnjql qntmrq Prqtesm Smqsmi qrjulq [email protected] 996666799
> juntur qntmrq Prqtesm Rqvitejq Jqllepqlli [email protected] 983
> jooty qntmrq Prqtesm Sqtti Kumqr  [email protected] 986663,
> West jotqvqri (Eluru) qntmrq Prqtesm Rqm Prqsqt [email protected] 59
> Mqncmeriql qntmrq Prqtesm Smqntmilql jqmlotm 
> [email protected] 898575
> Kmqmmqm qntmrq Prqtesm Lqksmmqn Rqo jqtipqrtmy [email protected]
> Kurnool (Nemru Nqjqr) qntmrq Prqtesm lqntulmqi Iqlql mussqin
> [email protected] 986, 8958575, 8958575
>
>
>
> my attempt:
> perl -ple 's/^.*\s(\w*@\w*.\w+).*$/$1/'  <file>
>
> my result:
> [email protected]
> [email protected]
> [email protected]
> [email protected]
> [email protected]
> Wqrqnjql qntmrq Prqtesm Smqsmi qrjulq [email protected] 996666799
> [email protected]
> [email protected]
> [email protected]
> Mqncmeriql qntmrq Prqtesm Smqntmilql jqmlotm 
> [email protected] 898575
> [email protected]
> [email protected]
>
>
> please advise how should be my regex?
>
> thx.



Hi Rajeev,

Now making ar regular expression just for your example list I would say
this will work...

perl -ple 's/^.*\b(\w+@\w*.\w+)\b.*$/$1/' <file>

But I have done this before and I knwo that some people are funny and have
an email that looks like one of the below examples.

This is crap because of the dots [email protected] 234234234, 234523423
This is also crap because of the dots
[email protected], 234523423

You would get this out of your file:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Not to good now is it... therefore I would suggest doing the following
pretty much fool proof trick

perl -ple 's/^.*\s(.+@.+.\w+)\s.*$/$1/' <file>

This way you are saying that you want to capture all data preceded by a
space then some stuff an @ some more stuff a dot and some more stuff. This
should capture pretty much all emails in your file without any real
restrictions on the formatting making it quite easy to capture everything
that looks like an email address. :-)

Regards,

Rob

Reply via email to