Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

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

SOLVED
Go to Solution
Highlighted
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
Acclaimed Contributor Acclaimed Contributor

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...

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..