[wellylug] Getting Email addresses from a file with grep etc

Brian Boutel brian at boutel.co.nz
Fri Jun 4 18:10:24 NZST 2004


Joseph Booth (Mangee) wrote:

> Hi all.
> 
> Trying to add about four thousand (valid) email addresses to a list from 
> my mail archives. The mail is in a text file, and i'm trying to grep out 
> clean addresses for adding to a mail list
> I know it sounds highly spamish, but I assure you each of these email 
> address is from personal correspondence over the last year...  I would 
> download a email harvesting engine, but that seems wrong ;)
> 
> I've tried:
> 
> grep -ane @  ~/Desktop/mfest.txt
> 
> Which gives me each line with a @ in it.. and the rest of the crud..
> ....

> 
> I've been playing with variations on...
> $ grep -an '[a-z0-9._]+@[a-z.]+' ~/Desktop/mfest.txt
> But can't get anything to output...
> 
> Any better ideas?
> 
>
The problem is that by default, GNU grep uses basic regexps, so you need 
to escape "+" as "\+". Also, you need to escape "." as "\." as by itself 
it's a wildcard, even in basic regexps. So try

$ grep -n '[a-z0-9\._]\+@[a-z\.]\+' ~/Desktop/mfest.txt

or use egrep, and don't escape the "+"

$ grep -nE '[a-z0-9\._]+@[a-z\.]+' ~/Desktop/mfest.txt

If you just want the addresses, and not the rest of the stuff on 
matching lines,  use the -o option

$ grep -o '[a-z0-9\._]\+@[a-z\.]\+' ~/Desktop/mfest.txt

You can still get the line numbers if you want

$ grep -no '[a-z0-9\._]\+@[a-z\.]\+' ~/Desktop/mfest.txt

OK?

--brian





More information about the wellylug mailing list