Solved

GREP syntax for email addresses?

Posted on 2007-03-30
11
1,058 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

758 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

21 Experts available now in Live!

Get 1:1 Help Now