ksh help: list following 3 lines after find a keyword (195 Views)
Reply
Super Advisor
Hanry Zhou
Posts: 1,167
Registered: ‎01-11-2002
Message 1 of 4 (195 Views)
Accepted Solution

ksh help: list following 3 lines after find a keyword

[ Edited ]

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,768
Registered: ‎03-06-2006
Message 2 of 4 (126 Views)

Re: list following 3 lines after finding a keyword

[ Edited ]

Get yourself gnu grep and just use -A3.

Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,178
Registered: ‎05-29-2000
Message 3 of 4 (116 Views)

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.

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,768
Registered: ‎03-06-2006
Message 4 of 4 (101 Views)

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.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation