Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using grep to find all emails

Tags:

linux

grep

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 !

like image 901
AntonAL Avatar asked May 24 '10 16:05

AntonAL


People also ask

How do I use grep to search?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.

Can grep search directories?

The grep command is a useful Linux command for performing file content search. It also enables us to recursively search through a specific directory, to find all files matching a given pattern. Let's look at the simplest method we can use to grep the word “Baeldung” that's included in both .


1 Answers

Here is another example

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

This variant works with 3 level domains.

like image 63
mosg Avatar answered Sep 18 '22 19:09

mosg