Home » Questions » Computers [ Ask a new question ]

How to grep only selected words from a file?

How to grep only selected words from a file?

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

Asked by: Guest | Views: 282
Total answers/comments: 2
bert [Entry]

"@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"
bert [Entry]

"@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"