1825551 Members
2738 Online
109681 Solutions
New Discussion юеВ

script

 
SOLVED
Go to solution
Satish Y
Trusted Contributor

script

Hi All,

I am writing a script that finds error messages from logs. I am finding by greping error msg number. The thing I am looking for is I want to grep 2,3 extra lines(following line containing my pattern). Can anybody tell me how to do that.

Thanks in advance.

Note: I don't know perl. I want it without using perl.

Cheers...
Satish.
Difference between good and the best is only a little effort
11 REPLIES 11
Sridhar Bhaskarla
Honored Contributor

Re: script

Satish,

Then you may want to use sed. Let's say your Error No is defined as $ERR and your pattern is $PATTERN, then use sed like this

sed -n '/'$ERR'/,/'$PATTERN'/p' your_log_file

This may not exactly give what you want. You can make some modifications to the above command and achive whatever you want.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Satish Y
Trusted Contributor

Re: script

Hi Sridhar,

My error number and pattern r not different they r same, means i am using error number as my pattern.

Regards,
Satish.
Difference between good and the best is only a little effort
Volker Borowski
Honored Contributor
Solution

Re: script

Just a try :

# cat trick.awk

BEGIN { pri = 0 }
{
if ( match($0, PAT ) > 0 )
pri = 3
if ( pri > 0 )
{
print
pri = pri - 1
}
}

# cat test.txt

dkjh dfkgj hfgkdjh gdkj ghdkgjdf
df gkjhfgkfj kfjh dkfgj hdkgh fdkg
fdl gkjfldgkjdflgkjdfgl TARGET
1
2
3
4

# awk -v PAT=TARGET -f trick.awk test.txt

fdl gkjfldgkjdflgkjdfgl TARGET
1
2


may be you need to put the calling commandline in a script to pass PAT=$1 as a parameter.
Change "pri=3" if you want more lines after the hit.

Volker
John Waller
Esteemed Contributor

Re: script

Give Volker the 10 points , it is only small and it works a treat, it will help me out a loads with a similar problem I have.
harry d brown jr
Honored Contributor

Re: script

You could use the GNU grep, found here:

http://www.gnu.org/software/grep/grep.html

With GNU grep you get a lot more options. The "-A 1" with the "-B 1" options will display the line before the matching line, the matching line, and then the trailing line.

Live Free or Die
Sridhar Bhaskarla
Honored Contributor

Re: script

Satish,

It would be nice if you can cat few lines of your log file and tell us what you want to extract. Check up the following script.

#!/usr/bin/ksh

ERR=$1
LEN=$2

/usr/bin/sed -n '/'$ERR'/,$p' your_log |sed -n '1,'$LEN'p'

Now you can pass two arguments to this script
Arg1=Your Error Number and LEN=Number of lines you want to print.

Let me know if this helped you.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Satish Y
Trusted Contributor

Re: script

Thanks for all your responses,

Thanks Volker for a nice script...

Excellent Sridhar, it worked well...
I also want to grep 2 lines before the pattern, is it possible?...
One more thing I didn't understand what $p does in ur sed statement. Can u explain?.

Waiting for reply....

Cheers...
Sat.
Difference between good and the best is only a little effort
Sridhar Bhaskarla
Honored Contributor

Re: script

Satish,

Yes.. there is a straight method but I don't remember it now. I can tell you a work around.

Try this way.

#!/usr/bin/ksh

ERR=$1
PRE=`echo "$2 + 1"|bc`
POST=$3

/usr/bin/sed -n '1,/$ERR/p' your_log |tail -$PRE > result

/usr/bin/sed -n '/'$ERR'/,$p' your_log |sed -n '2,'$POST'p' >> result

p is the print macro in sed.

I do not have a system in front of me. So I am not sure if it works.. But you can adjust and make it to work.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Volker Borowski
Honored Contributor

Re: script

OK,
another try. This is difficult, because you need to store the lines before your hit, because you do not really know, if you'll need them.
... and if there is a double hit, you may not reprint an already printed line.
If you want do seperate the hits, you might insert
print "-------"
before you print DELTA2.
For more leading lines, add more DELTAx, but to handle the double hits correctly, the number of printed lines after the hit (pri=3) needs to be greater or equal the number of DELTAx lines.

Hey these puzzles are fun :-)

here we go:
#########################
BTW: Does anyone know, why this textbox is eliminating my leading blanks ?

Volker

# cat trick.awk

BEGIN { pri = 0 }
{
if ( match($0, PAT ) > 0 )
{
if ( pri == 0 )
{
print DELTA2
print DELTA1
}
pri = 3
}
if ( pri > 0 )
{
print
pri = pri - 1
}
DELTA2 = DELTA1
DELTA1 = $0
}

# cat test.txt

no printline
second before target 1 (easy)
first before target 1 (easy)
first:first TARGET garbage
1
2
3
no printline
no printline
no printline
second before target 2 (easy)
first before target 2 (easy)
second:second TARGET garbage
1
mean:mean TARGET garbage
2
3
no printline
no printline
no printline
no printline

# awk -v PAT=TARGET -f trick.awk test.txt

second before target 1 (easy)
first before target 1 (easy)
first:first TARGET garbage
1
2
second before target 2 (easy)
first before target 2 (easy)
second:second TARGET garbage
1
mean:mean TARGET garbage
2
3

#
Satish Y
Trusted Contributor

Re: script

Thanks all for your great responses....

Cheers...
Satish.
Difference between good and the best is only a little effort
Bill McNAMARA_1
Honored Contributor

Re: script

well done Volker, I was just about to post for the same thing..
nice script!

Later,
Bill
It works for me (tm)