grep – Get all email addresses from file

A nice grep/egrep command to get all email addresses from a textfile.

grep -E -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" filename.txt
egrep -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" filename.txt

You may find duplicates in the output of the command. [ uniq ] command will help remove duplicates. And to sort all the extracted emails we will use [ sort ] command.

# uniq emails and sort
egrep -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" filename.txt | uniq | sort