Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 02:07 AM
тАО09-05-2001 02:07 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 02:14 AM
тАО09-05-2001 02:14 AM
Re: script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 02:30 AM
тАО09-05-2001 02:30 AM
Re: script
My error number and pattern r not different they r same, means i am using error number as my pattern.
Regards,
Satish.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 03:19 AM
тАО09-05-2001 03:19 AM
Solution# cat trick.awk
BEGIN { pri = 0 }
{
if ( match($0, PAT ) > 0 )
pri = 3
if ( pri > 0 )
{
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 04:20 AM
тАО09-05-2001 04:20 AM
Re: script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 04:40 AM
тАО09-05-2001 04:40 AM
Re: script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 07:16 AM
тАО09-05-2001 07:16 AM
Re: script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 10:20 PM
тАО09-05-2001 10:20 PM
Re: script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 11:23 PM
тАО09-05-2001 11:23 PM
Re: script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2001 11:27 PM
тАО09-05-2001 11:27 PM
Re: script
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 )
{
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
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2001 03:41 AM
тАО09-06-2001 03:41 AM
Re: script
Cheers...
Satish.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-14-2001 01:32 AM
тАО09-14-2001 01:32 AM
Re: script
nice script!
Later,
Bill