Anis_Stack
Anis_Stack

Reputation: 3452

extract email adress from csv file

I want to extract all email address from csv file. how to do this action. by the way the location of the email is not organized by column or by row.

in fact there is no regular format, but I give you an example

 "bla bla bla bla Website: www.mysite.com ; Email: [email protected]";usa; 

so the question is how to extract the email address from this sentence ?

Upvotes: 0

Views: 3932

Answers (3)

Anis_Stack
Anis_Stack

Reputation: 3452

extract email adsress from folder full of csv file; just using perl

cat *.csv > all.csv 
perl -wne'while(/[\w\.\-]+@[\w\.\-]+\w+/g){print "$&\n"}' all.csv | sort -u > output.txt 

Upvotes: 0

Anis_Stack
Anis_Stack

Reputation: 3452

thank for all; I found the correct answer for my question, it is :

grep -E -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" file.csv | sort -u > email_list 

Upvotes: 10

Paulo Scardine
Paulo Scardine

Reputation: 77339

Please adjust the regular expression if needed (\S+@\S+ probably too simple):

grep -o -P '\S+@\S+' input.csv

From man grep:

-o, --only-matching
      Print only the matched (non-empty) parts of a matching line, 
      with each such part on a separate output line.
-P, --perl-regexp
      Interpret PATTERN as a Perl regular expression (PCRE, see below).  
      This is highly experimental and grep -P may warn of unimplemented features.

To sort and skip duplicates:

grep -o -P '\S+@\S+' input.csv | sort -u

Upvotes: 3

Related Questions