Re: list following 3 lines after finding a keyword (1832 Views)
Reply
Super Advisor
Hanry Zhou
Posts: 1,167
Registered: ‎01-11-2002
Message 1 of 4 (1,926 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
Acclaimed Contributor
Dennis Handly
Posts: 25,188
Registered: ‎03-06-2006
Message 2 of 4 (1,857 Views)

Re: list following 3 lines after finding a keyword

[ Edited ]

Get yourself gnu grep and just use -A3.

Honored Contributor
Bill Hassell
Posts: 14,210
Registered: ‎05-29-2000
Message 3 of 4 (1,847 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.

Acclaimed Contributor
Dennis Handly
Posts: 25,188
Registered: ‎03-06-2006
Message 4 of 4 (1,832 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.

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.