0

I want to do server.log analysis for error and i want before 10 line and after 10 line of that error so below script is giving error in Hp-ux,please provide alternate solution:

grep -A 10 -B 10 "ERROR" server.log >> /tmp/Test.log

2 Answers2

0

Hope this helps

grep -A10 -B10 -P "ERROR" server.log >> /tmp/Test.log
Faizan Younus
  • 793
  • 1
  • 8
  • 13
  • 1
    grep: illegal option -- A grep: illegal option -- 1 grep: illegal option -- 0 grep: illegal option -- B grep: illegal option -- 1 grep: illegal option -- 0 grep: illegal option -- P usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern_list... [-f pattern_file...] [file...] usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern_list...] -f pattern_file... [file...] usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file...] – Chirag Dhaduk Dec 01 '16 at 09:46
0

Since the old grep hasn't the convenient -ABC options, the solution is a little more complicated:

grep -n "ERROR" server.log | sed 's/\([0-9]*\):.*/\1-10<=NR\&\&NR<=\1+10{print;next}/' \
                           | awk -f- server.log >>/tmp/Test.log

This uses sed -n to get the line numbers and a sed-generated awk script to print the context.

Armali
  • 18,255
  • 14
  • 57
  • 171