grep for text and lines above and below

hi all,
in ksh i wanted to grep for some text and also include a few lines above and below that text. how do i do that ???

i am trying to get my script to print out request and response from the server log file.

getting the request is easy, i just do a grep -i "request" from log but i need to pair that request with its response which is just 2 lines below the request.

thanks in advance.

ksh, not sure you can with grep.

Perhaps 15-20 lines should be sufficient.
Sometimes, there are 'tricks' to fool a system into providing more lines.

Depends on which version of grep you have:

$ grep --version
grep (GNU grep) 2.5.1

Under the help:
Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM


$ cat file
0
10
34
34
54
122
58747
54
99
2
1
45

$ grep -C1 58747 file
122
58747
54

You should do a search on this forum to get alternate versions if you don't have GNU grep

the part i am interested in from the log file is of the following structure :

REQUEST <SOAP-ENV:Envelope ...... </SOAP-ENV:Envelope>
[12/16/08 13:17:01:976 NZDT] 1c2c259 SystemOut O
[12/16/08 13:17:02:311 NZDT] 1c2c259 SystemOut O RESPONSE [335 millis]

ta.

input:

likljasdf
lkasjf
line 1
line 2
line 3
asdfjkl
qwioru

output:

line 1
line 2
line 3

code:

sed -n '/line 2/ !{
h
}
/line 2/ {
H
n
H
x
p
}' a