GREP syntax for email addresses?

Posted on 2007-03-30
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:

<>... Deferred: Connection timed out with
<>... Deferred: Connection timed out with
<>... Deferred: 452 4.2.2 Message would exceed quota

IOW I want the grep to select just "" or "" 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.

Question by:hergeekness
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
LVL 84

Expert Comment

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

Author Comment

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.



Accepted Solution

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.


Expert Comment

ID: 18828880
You could also use awk:

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


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

where em is the file you are processing.

Author Comment

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!

LVL 48

Expert Comment

ID: 18833764
GNU grep has the

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


Author Comment

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

Expert Comment

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

Author Comment

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

Expert Comment

ID: 18834228
Just type in

grep -o

to find out if OS X supports it.


Author Comment

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.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Syslogd is a utility that traps and logs messages sent by running processes. It is configured with the syslog.conf file, which consists of lines containing a pair of fields: "the selector field which specifies the types of messages and priorities to…
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
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…

695 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