Pattern matching help! grep emails from file!

D

danpres2k

Hello, I have a file with email address at a lot of junk data. I want
to get the email addresses out of that file so that each email address
is stored at a new line. I am trying to do (e-mail address removed)
substitution:
$filestring=<FILE>;
$filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;

The file is like:
"testin" (e-mail address removed), <testing>[email protected]
(e-mail address removed)
"(e-mail address removed)"

Expected output:
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

Thanks guyz.
 
S

Shawn Milochik

Hello, I have a file with email address at a lot of junk data. I want to
get the email addresses out of that file so that each email address is
stored at a new line. I am trying to do (e-mail address removed) substitution:
$filestring=<FILE>;
$filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;

The file is like:
"testin" (e-mail address removed), <testing>[email protected] (e-mail address removed)
"(e-mail address removed)"

Expected output:
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

Thanks guyz.

Two things:

1. Do you really want 2 newlines for each output?

2. Since the first regex is matching the e-mail address and
ONLY the e-mail address, you're actually telling the s/// to
search the entire string for an e-mail address and substitute
the e-mail address with itself, not, as you intend, to substitute
the entire string with itself. You want to add a .* after the closing
parenthesis, maybe.

Instead of:
$filestring = s/(\w+\@\w+\.\w+)/\n$1\n/;
Try:
$filestring = s/(\w+\@\w+\.\w+).*/\n$1\n/;

Or Possibly:
$filestring = s/.*(\w+\@\w+\.\w+).*/\n$1\n/;

Untested, but I had a similar problem recently, and the
principle is the same.

Shawn
 
D

danpres2k

Shawn,

Thanks for your help. But I couldn't use that as well. I am getting
null value for $filestring when I am printing it:

$filestring = <FILE>;
$filestring = s/.*(\w+\@\w+\.\w+).*/$1/;
print $filestring;

Got any suggestion?
Thanks.
 
D

danpres2k

Thanks again Shawn, It did work but only printed a part of the last
email in the first line. how do i go about the newline chars in the
$filestring? i am storing the string from the file handle in
$filestring. is this correct?

thanks.
d
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top