Extract email addresses from a line with Grep
This is certainly a classic bit of regex wizardry but since it took me a few minutes of searching and can be valuable in a variety of contexts, it might be valuable to you too…
grep -o ‘[[:alnum:]+\.\_\-]*@[[:alnum:]+\.\_\-]*’
I needed it for extracting the adresses returning a 550 from my Postfix logs. But then I found that Sympa, my mailing list management system, handles bounces automatically very well using a scoring algorithm that the list administrator can optionally override.
We shall call this process “serendipitous ignorance“…
While we are trying to make sense of regular expressions, those curious about them and wishing for an introduction geared toward audiences other than the beard and sandals systems administration crew may appreciate the examples provided in “Egrep for Linguists“.
And yes, I do indulge in sandals and facial pilosity in the hope of mastering regexes one day…
5 responses to “Extract email addresses from a line with Grep”
Leave a Reply
You must be logged in to post a comment.
My friend Vador found a good ressource for further reading at http://www.regular-expressions.info/email.html – don’t miss the 6343 characters long RFC822 regex !
You saved my day (or maybe just my afternoon). Right on the spot for spamming friends with my New Year greetings. Thanks !
seems @bla.bla are also matched
so I changed it as:
grep -o -E ‘([[:alnum:]+\.\_\-])+@([[:alnum:]+\.\_\-])+’
Good catch – thanks !
Nice. I needed that to extract twitter usernames from a file. This works for me.
Cheers!