Solved

GREP syntax for email addresses?

Posted on 2007-03-30
11
1,065 Views
Last Modified: 2010-08-05
What is the best GREP syntax to select an email address? In the file(s) I'm searching (using Terminal in Mac OS X ... uses bash), I want it to select just the e-mail address in the only line in the file that *begins* with an e-mail address, such as:

<dfedfe@msn.net>... Deferred: Connection timed out with msn.net.
...or...
<dorega@human.co.uk>... Deferred: Connection timed out with human.co.uk.
...or...
<rocky@e-mailanywhere.com>... Deferred: 452 4.2.2 Message would exceed quota

IOW I want the grep to select just "dfedfe@msn.net" or "dorega@human.co.uk" etc. I don't want the < and the > and I don't want anything else on the line.

I already know how to tell it to find the line itself, remove dupes and put it in a text file:
| grep "^<" | sort -u > ~/Desktop/bounces.txt

but as I said, I just want the actual email addy's, not the whole line, added to bounces.txt.

thx
0
Comment
Question by:hergeekness
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 18827929
perl -lne 'print $1 if/^<(.*?)>/' file >> bounces.txt.
0
 
LVL 4

Author Comment

by:hergeekness
ID: 18828645
I get an error with that. I'm sure it has something to do with Terminal itself, no?

After cd'ing to my mailbox folder, I entered your command (after entering the mailbox -- filename "zterminal" -- in which I wanted to run the search):

 cat zterminal | perl-lne 'print $1 if/^<(.*?)>/' file >> bounces3.txt

I got this response from bash:
-bash: perl-lne: command not found

?

and ... there's no way to do this with grep? It looks so close. I tried this:
cat zterminal | grep "$1 ^<(.*?)>" | sort -u > ~/Desktop/bounces4.txt

... and got a blank file. I flailed a bit and tried:
cat zterminal | grep $1 "^<(.*?)>" | sort -u > ~/Desktop/bounces4.txt

... and got another blank file.

How can I tell grep to take the results of the parentheses and put them in the external .txt file? I'm thinking it has something to do with my pipes? I don't know know how to insert a "replace" in the bash command line I guess is what I'm saying. (I'm slightly more familiar with using grep in programs where there's a Find field and a Replace field).

I'm fine doing it with a perl command, if I can get it to work, but I was hoping for some grep way to do it since I'm trying to get more familar with the syntax.

hg

0
 
LVL 3

Accepted Solution

by:
mcmarks earned 250 total points
ID: 18828836
grep can't extract text from a line.  It only returns complete lines.  In your response it looks like you missed the space between perl and -lne.  This line works for me:

cat em | perl -lne 'print $1 if/^<(.*?)>/' >> bounces.txt

where my source file is named em


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:mcmarks
ID: 18828880
You could also use awk:

cat em | awk 'BEGIN{FS="<|>"} ; /^<(.*)>/{print $2}'

or

awk 'BEGIN{FS="<|>"} ; /^<(.*)>/{print $2}' em

where em is the file you are processing.
0
 
LVL 4

Author Comment

by:hergeekness
ID: 18828987
ah ... "grep only returns complete lines" ... thanks. That's a limitation of how it works in bash or from any CLI? (that you can't tell it to just return the result of the pattern match?)

You're right, I was missing a space. I ran the same perl command (using my source file name) that you provided and it didn't cause an error this time.

I had to add some other code to get it to work in Terminal ... some mumbo jumbo about linefeeds that a friend told me about, otherwise the file it creates is blank. Here's what I used in the end ... the source file is a Eudora mailbox called "bouncing":

cat bouncing | tr "^M" "
" | perl -lne 'print $1 if/^<(.*?)>/' | sort -u >> ~/Desktop/tobounce4.txt

Worked great! Thanks!

hg
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18833764
GNU grep has the

-o --only-matching option to only display the pattern matched by the grep string.

0
 
LVL 4

Author Comment

by:hergeekness
ID: 18834063
Tintin, so would it be possible to use the -o command with grep in terminal?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18834106
If OSX has GNU grep or a version of grep that has the -o option, then the answer is yes.
0
 
LVL 4

Author Comment

by:hergeekness
ID: 18834114
Gee that sounds like a good question for the OS X section on experts exchange.... ;-)
0
 
LVL 48

Expert Comment

by:Tintin
ID: 18834228
Just type in

grep -o

to find out if OS X supports it.

0
 
LVL 4

Author Comment

by:hergeekness
ID: 18834270
yes, it says it does.

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the filename for each match
  -h, --no-filename         suppress the prefixing filename on output
      --label=LABEL         print LABEL as filename for standard input
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output


thanks for the tip! I'll experiment.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Deploystudio is a system which can be used to deploy OSX clients and servers within the small/medium or large business environments. The system is built onto of the OSX Server NetBoot system and uses images & workflows as its core assets. While work…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question