Solved

GREP syntax for email addresses?

Posted on 2007-03-30
11
1,060 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
 
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I get rid of mic.com pop ups on my MAC? 9 1,135
How to Insert a File Using Text Editor 9 80
Mac OS gmail saves mail twice after replacement rule 6 39
mac maintenance 6 34
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

896 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now