Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract email addresses from text file using regex with bash or command line

Tags:

regex

linux

bash

How can I grep out only the email address using a regex from a file with multiple lines similar to this. (a sql dump to be precise)

Unfortunately I cannot just go back and dump the email column at this point.

Example data:

62372,35896,1,cgreen,Chad,Green,[email protected],123456789,0,,,,,,,,,3,Blah,,2013-05-02 17:42:31.659574,164842,,0,0

I have tried this but it did not work:

grep -o '[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}' file.csv
like image 714
Arringar1 Avatar asked Nov 12 '13 22:11

Arringar1


People also ask

How do I extract email addresses from a text file?

Email Extractor - Online Tool for Extracting Email Address Simply insert your source text content that includes the email addresses in provided text-box, click the "Extract Emails" button, and you'll get a clean list of unique email addresses.


2 Answers

If you still want to go the grep -o route, this one works for me:

$ grep -i -o '[A-Z0-9._%+-]\+@[A-Z0-9.-]\+\.[A-Z]\{2,4\}' file.csv
[email protected]
$ 

I appear to have 2 versions of grep in my path, 2.4.2 and 2.5.1. Only 2.5.1 appears to support the -o option.

Your regular expression is close, but you're missing 2 things:

  • regular expressions are case sensitive. So you can either pass -i to grep or add extra a-z to your square bracket expressions
  • The + modifiers and {} curly braces appear to need to be escaped.
like image 119
Digital Trauma Avatar answered Oct 04 '22 00:10

Digital Trauma


If you know the field position then it is much easier with awk or cut:

awk -F ',' '{print $7}' file

OR

cut -d ',' -f7 file
like image 28
anubhava Avatar answered Oct 03 '22 23:10

anubhava