Solved

GREP syntax for email addresses?

Posted on 2007-03-30
11
1,062 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
  • 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

785 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