Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Search for an expression and capture few lines above & below it

SOLVED
Go to solution
rk_12345
Occasional Contributor

Search for an expression and capture few lines above & below it

Hi All,

 

I have a file..my aim is to:

1. Search for the line containing a particular expression

2. Grab 3 lines above that expression

3. Grab 5 lines below that expression

 

Please let me know how can this be achieved?

 

Many a thanks..

2 REPLIES
James R. Ferguson
Acclaimed Contributor
Solution

Re: Search for an expression and capture few lines above & below it

Hi:

 

You can roll your own or you can use a GNU 'grep'.  See here for some ways to build your own:

 

http://h30499.www3.hp.com/t5/System-Administration/grep-for-string-but-include-lines-above-and-below/m-p/3703431#M249759

 

See here for a GNU 'grep':

 

http://hpux.connect.org.uk/hppd/hpux/Gnu/grep-2.9/

 

If you chose to use the Porting Centre's offering, be sure to install the dependencies.  This is easily done by first installing the 'depothelper' from there.

 

Regards!

 

...JRF...

Highlighted
rk_12345
Occasional Contributor

Re: Search for an expression and capture few lines above & below it

Thanks James...I tried a script find_before_after...works good.

Also installed the GNU grep and its dependencies. That works like wow..