Extract e-mail addresses on a page

Hi I normally ask questions on coding but I think there is a code that can do this. I have regular text throughout my file and I want to extract all e-mail addresses from it (rather than going and searching each one).

E-mails all have @ so I assume there is a way.

Thanks

Phil

Will this serve your purpose?

awk ' { for(i=1; i<=NF; i++) { if($i ~ /@/) print $i } } ' filename
1 Like

Try this:

grep -oiE '\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b' infile
1 Like

thanks, both methods work well on my Unix system however I was wondering if I could get a PERL version.

try:

perl -e 'while(<>) {@words=split; foreach $w (@words) {print "$w\n" if $w=~/.[@]./}}' infile
1 Like

Thank you! This works very well but I get a "." at the end of some e-mails. For instance if the email is bob@bob.com. then I get bob@bob.com. and not just bob@bob.com

Any solution around this would be fine. Thanks

perl -e 'while(<>) {@words=split; foreach $w (@words) {$w=~s/\.$//;print "$w\n" if $w=~/.[@]./}}' infile
1 Like