O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Parsing e-mail addresses and URLs from text

Parsing required text from a given file is a common task that we encounter in text processing. Items such as e-mail, URL, and so on can be found out with the help of correct regex sequences. Mostly, we need to parse e-mail addresses from a contact list of a e-mail client which is composed of many unwanted characters and words or from a HTML web page.

Getting ready

This problem can be solved with utilities egrep.

How to do it...

The regular expression pattern to match an e-mail address is:

egrep regex:[A-Za-z0-9.]+@[A-Za-z0-9.]+\.[a-zA-Z]{2,4}

For example:

$ cat url_email.txt 
this is a line of text contains,<email> #slynux@slynux.com. </email> and email address, blog "http://www.google.com", test@yahoo.com ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required