LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Advanced Grepping (http://www.linuxquestions.org/questions/linux-general-1/advanced-grepping-547317/)

keysorsoze 04-19-2007 08:58 AM

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?

nmh+linuxquestions.o 04-19-2007 09:28 AM

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

druuna 04-19-2007 09:39 AM

Hi,

sed can do what you ask:

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

Hope this helps.

keysorsoze 04-19-2007 10:02 AM

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.

keysorsoze 04-19-2007 07:17 PM

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.

jschiwal 04-19-2007 07:37 PM

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.

keysorsoze 04-19-2007 08:59 PM

Thank you for the great explanation. Any tips on what to study to be a guru sed wielding beast such as yourself?

druuna 04-20-2007 12:26 AM

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

jschiwal 04-20-2007 04:25 AM

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.

keysorsoze 04-20-2007 05:47 AM

Thanks for the tips on the books guys I'll be definitely picking these two up.

nx5000 04-20-2007 06:00 AM

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


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