tags:

views:

102

answers:

2

Hi, How to properly construct regular expression for "grep" linux program, to find all email in, say /etc directory ? Currently, my script is following:

grep -srhw "[[:alnum:]]*@[[:alnum:]]*" /etc

It working OK - a see some of the emails, but when i modify it, to catch the one-or-more charactes before- and after the "@" sign ...

grep -srhw "[[:alnum:]]+@[[:alnum:]]+" /etc

.. it stops working at all

Also, it does't catches emails of form "[email protected]"

Help !

+5  A: 

grep requires most of the regular expression special characters to be escaped - including +. You'll want to do one of these two:

grep -srhw "[[:alnum:]]\+@[[:alnum:]]\+" /etc

egrep -srhw "[[:alnum:]]+@[[:alnum:]]+" /etc
Jefromi
A: 

Here is another example

grep -Eiorh '([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6})' "$@" * | sort | uniq > emails.txt

This variant works with 3 level domains.

mosg