Print selection of line based on line number

Hi Unix gurus

Basically i am searching for the pattern and getting the line numbers of the grepped pattern. I am trying to print the series of lines from 7 lines before the grepped line number to the grepped line number.

I am trying to use the following code. but it is not working.

cat filename | grep -n pattern | sed 's/:/ /g' | awk 'NR==$1-7,NR==$1'

Please help

Use ggrep for this, ggrep can be found under samba:

/usr/sfw/bin/ggrep "PATTERN" -A 0 -B 7 yourfile

Besides, you can print line number of 4th occurance of a pattern "mohan":

nawk '$0~/mohan/{i++} i==4{print NR;exit}' yourfile

Using these knowledge you can try to solve your issue,

Regards

Hi thanks for your reply

Both ggrep and nwak is not available in the system.

Please any other way using awk.

Try

$ grep -n pattern file | grep -B 6 .