LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-19-2007, 08:58 AM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
Advanced Grepping


Hello I am looking for a way to grep our mailq for specific messages in the following way.
For example:


Bash#mailq


223F477F4 1415 Mon Apr 16 23:59:20 someuser@somedomain.com
(lost connection with somedomain.com[0.0.0.0] while receiving the initial server greeting)
some@somedomain.com


cat maillog | grep someuser@domain.com


223F477F4 1415 Mon Apr 16 23:59:20 someuser@somedomain.com



The above results show too little information for anaylsis



How do I perform a grep that displays all lines of the message my goal is to perform a grep
such as cat maillog | grep someuser@domain.com and get in return:



223F477F4 1415 Mon Apr 16 23:59:20 someuser@somedomain.com
(lost connection with somedomain.com[0.0.0.0] while receiving the initial server greeting)
some@somedomain.com



223F477F4 1415 Mon Apr 16 23:59:20 someuser@somedomain.com
(lost connection with somedomain.com[0.0.0.0] while receiving the initial server greeting)
some@somedomain.com



223F477F4 1415 Mon Apr 16 23:59:20 someuser@somedomain.com
(lost connection with somedomain.com[0.0.0.0] while receiving the initial server greeting)
some@somedomain.com


This would equal to all messages from someuser@somedoamin.com along with the last 3 or for trailing lines that show the lost connection and the recipient. Is there a way to grep for this?
 
Old 04-19-2007, 09:28 AM   #2
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
Thumbs up probably not what you want...

Code:
man grep
GREP(1)                                                                
...
OPTIONS
       -A NUM, --after-context=NUM
              Print NUM  lines  of  trailing  context  after
              matching  lines.  Places  a  line  containing
               --  between contiguous  groups  of matches.
If you want to get a variable number of lines, you may have to figure out a complex regexp and use 'grep -e' or egrep (if supported)

You could also take a look at: http://www.kclug.org/pipermail/kclug...ne/028106.html
 
Old 04-19-2007, 09:39 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

sed can do what you ask:

sed -n '/someuser@somedomain.com/,/^$/p' infile

Hope this helps.
 
Old 04-19-2007, 10:02 AM   #4
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks I think this is what I need, however I don't see the -A num for Solaris in the man pages of grep but I am sure there is another method.
 
Old 04-19-2007, 07:17 PM   #5
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
WOW! mailq | sed -n '/someuser@somedomain.com/,/^$/p' worked great! Now if only I understood what this meant. Thanks a lot I'll have to do some serious reading on the power of the sed.
 
Old 04-19-2007, 07:37 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Quote:
Originally Posted by keysorsoze
WOW! mailq | sed -n '/someuser@somedomain.com/,/^$/p' worked great! Now if only I understood what this meant. Thanks a lot I'll have to do some serious reading on the power of the sed.
The '/something/,/somethingelse/' selects a range that the rest of the command operates on, which in this case is just to print in. The /^$/ selects a blank line as the end of the range.

In linux, you can do something similar to select a section from the lspci output:
/sbin/lspci -v | sed -n '/Broadcom/,/^$/p'
The -n option to sed suppresses output. So only lines with the (p)rint command are output.

You can use sed like this for any output or config file that seperates sections of text with a blank line. This is a commonly used pattern.
 
Old 04-19-2007, 08:59 PM   #7
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thank you for the great explanation. Any tips on what to study to be a guru sed wielding beast such as yourself?
 
Old 04-20-2007, 12:26 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

I personally like OReilly's Sed&Awk book. It starts easy and goes all the way to the advanced topics.

There's also info online:

Seder's grabbag
The SED $HOME
Sed. An introduction
 
Old 04-20-2007, 04:25 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
For awk, the book (info awk), that comes with the package is excellent. It is called "GAWK: Effective Awk Programming". I installed the source, and ran "./configure" and "make pdf" to generate a printable version from the .texi source.
 
Old 04-20-2007, 05:47 AM   #10
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks for the tips on the books guys I'll be definitely picking these two up.
 
Old 04-20-2007, 06:00 AM   #11
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
As druuna said:
http://sed.sourceforge.net/

I always refer back to this one.


Especially it points to one easy and interesting:
http://www-106.ibm.com/developerwork...ry/l-sed1.html
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grepping a file for Text going down. keysorsoze Linux - Newbie 3 01-17-2007 09:06 AM
grepping all manfiles pgrodt Linux - Software 8 12-01-2006 10:40 AM
question about grepping something Grafbak Programming 6 03-01-2005 01:28 PM
grepping last word of output rajatgarg Programming 3 11-25-2003 10:41 AM
Not grepping emails right mikeyt_333 Linux - General 11 04-13-2003 02:48 PM


All times are GMT -5. The time now is 07:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration