Grep string but also it will show the next 5 lines

Hi experts,

I want to grep a number 9366109380 from a file but it will also show me the next 5 lines. Below is the example-

when i grep 989366109380, i can also see the next 5 lines.

Line 1. <fullOperation>MAKE:NUMBER:9366109380:PPAY2;</fullOperation>
Line 2. <starttime>20081003000047</starttime>
Line 3. <stoptime>20081003000047</stoptime>
Line 4. <fullResult>940120105;;</fullResult>
Line 5. <status>FAILED</status>

Please help..

//Purple

First, try GNU "grep -A 5 989366109380". If you don't have and can't get GNU grep (fileutils package), then try:

awk '/989366109380/,printed==5 { ++printed; print; }'

There are probably other solutions with sed.

awk '/989366109380/,printed==5 { ++printed; print; }'

above not giving any output :frowning:

pleaese tell me other way??

  1. Your sample input gave a different number than the one I used. Did you use the right number??
  2. Did you provide an input filename?

I guess you mean printed==4; also, the example has 9366109380 (remove the first two digits from the awk script)

check the -A option of grep..Example shown below...Grepped on "bash" keyword and printed next 5 lines.

[root@iqcharlie nua7]# grep -A 5 bash create_current.conf 
#!/bin/bash
echo "Please enter the hostname"
read hostname
echo "Please enter the ipaddress"
read ip
echo "Please enter the DB server name"

Heh, his example showed the next four lines, but he wrote the next 5. :stuck_out_tongue:

OH...i did not input the filename.. I put the file name as below and get the permission denied !!!!

bash-2.05$ 2008*log | awk '/989358666162/,printed==5 { ++printed; print; }'
bash: ./2008-08-12.0.log: Permission denied
bash-2.05$ ls -ltr 2008-08-12.0.log
-rw-r--r-- 1 root other 22595 Aug 20 04:45 2008-08-12.0.log

Why on earth are you trying to execute your log file? You'd be better off adding "2008*log" to the end of the awk command, like you would grep.

Why is the grep option not working in your case..?

HMM...that was great mistake...i should execute that logfiles on heaven...:smiley: