Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

ksh help: list following 3 lines after find a keyword

SOLVED
Go to solution
Hanry Zhou
Super Advisor

ksh help: list following 3 lines after find a keyword

I have a large files, and some lines include a keyword that I am looking for. Further, I wanted to list next 3 lines following the line that  includes the keywork.

 

Thank you very much for your help!

 

 

P.S. This thread has been moved from HP-UX > System Administration o HP-UX > languages. - Hp Forum Moderator

none
3 REPLIES
Dennis Handly
Acclaimed Contributor
Solution

Re: list following 3 lines after finding a keyword

Get yourself gnu grep and just use -A3.

Highlighted
Bill Hassell
Honored Contributor

Re: ksh help: list following 3 lines after find a keyword

In case your site doesn't allow open source software outside of hp.com, you can use awk:

 

awk '/pattern/{print;getline;print;getline;print;getline;print}' filename

 

where pattern is whatever keyword(s) you are looking for, and filename is the name of the file.



Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: list following 3 lines after finding a keyword

>In case your site doesn't allow open source software

 

If you like cryptic commands you can use sed:

sed -e '
/pattern/ {
   n; n; n; p
}
d'

Note: sed uses REs and awk uses EREs.