Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to get a list of all the email addresses from a file.

I was trying to use

grep @ filename

but that returns the entire line.

Is there anyway I can get it to return only the email address and not the whole line?

share|improve this question
add comment

3 Answers 3

That's going to depend on the format of the file. For example, let's say the file has

email@example.com stuff you don't want
email2@example.com more stuff you don't want
email3@example.com and more

then awk '/@/{print $1}' would seem to be the obvious answer.

Post an example of the file format if that's not what you've got.

share|improve this answer
    
the file has email address semi-randomly occurring through out the file. –  null_radix Jan 25 '10 at 15:35
add comment

Note that properly matching any valid email address is a deep magic, so if you really want to catch everything with no false positives or negatives, you should use a regex someone else has written. But if you're just looking for a quick grep that is Good Enough, check out the -o option which will show only the matching text.

$ grep -Po '\S+@\S+\.\w+' yourfile.txt

That will catch some simple email addresses, along with some things that are not valid email addresses (like "@@@@.a"). Adjust your regex as appropriate. E.g., this one is more restrictive:

$ grep -Po '[\w+.]+@[\w.]+\.\w+' yourfile.txt
share|improve this answer
    
regexes really aren't the right tool for handling email addresses. I'm not aware of any which will correctly handle every potential variation on the full address. For BNF specifications, you really should be using a parser. –  gorilla Jan 22 '10 at 1:31
add comment
up vote 0 down vote accepted

@rbright this solution seems to work. the "-o" option is what I was missing! But I ended up just writing a php script before I got your response.

$handle = fopen("MYFILE.txt","r");

if ($handle) {  
    while (!feof($handle)) {
        $buffer = fgets($handle);

        $pieces = explode(" ",  $buffer);
        foreach($pieces as $piece){
            if( strstr($piece, '@' && $piece != " ")){              
           echo $piece;             
            }
        }   
    }
 fclose($handle);
} 

This is a fast and dirty but it will hold up until there is a @ that is not in a email address which is very unlikely in my situation

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.