"grep" several lines

"grep" several lines

Post by Jens Pederse » Thu, 04 Dec 2003 20:40:52



Hi,

Often I need to find a section of a large file. I want to do something
similar to grep but I also need e.g. the 5 lines proceeding the found line
and the 7 lines after.

OS is Solaris 8.

How is that done the easiest way, eg. using sed or awk?

Regards,

Jens

 
 
 

"grep" several lines

Post by Ed Morto » Thu, 04 Dec 2003 23:10:26



> Hi,

> Often I need to find a section of a large file. I want to do something
> similar to grep but I also need e.g. the 5 lines proceeding the found line
> and the 7 lines after.

> OS is Solaris 8.

> How is that done the easiest way, eg. using sed or awk?

GNU grep. e.g.:

grep -B5 -A7 whatever file

would give you the 5 lines Before and 7 lines After "whatever".

        Ed.

Quote:> Regards,

> Jens


 
 
 

"grep" several lines

Post by Jens Pederse » Fri, 05 Dec 2003 00:48:34


Thanks, but I'm not allowed to install anything onto this customer
server......

What's the easiest way using standard Solaris tools?

Rgds. Jens



> > Hi,

> > Often I need to find a section of a large file. I want to do something
> > similar to grep but I also need e.g. the 5 lines proceeding the found
line
> > and the 7 lines after.

> > OS is Solaris 8.

> > How is that done the easiest way, eg. using sed or awk?

> GNU grep. e.g.:

> grep -B5 -A7 whatever file

> would give you the 5 lines Before and 7 lines After "whatever".

> Ed.
> > Regards,

> > Jens

 
 
 

"grep" several lines

Post by Ed Morto » Fri, 05 Dec 2003 02:10:39



> Thanks, but I'm not allowed to install anything onto this customer
> server......

> What's the easiest way using standard Solaris tools?

Easiest is kinda subjective ;-) but you could do this:

_lineNr=`grep -n pattern file | cut -d: -f1`

to get the line number, then something like this (or use shell-specific
constructs):

_start=`expr $lineNr - 5`
_end=`expr $lineNr + 7`

to get the start and end points of the file segment you want, then this:

sed -n "${_start},${_end}p" file

to print that file segment. ALternatively, you could get the line number
range using awk:

awk '/pattern/{print NR-5 "," NR+7}' file

You could do it easily in awk with a buffer for all lines, but you may
run out of space (I think an "old" awk can just hold 4095 array elements):

awk '{buf[NR]=$0}/pattern/{n=NR}
        END{for(i=n-5;i<=n+7;i++)print buf[i]}' file

If you want to make a more robust version in awk, you'll need to create
a rollingh buffer of just the 5 preceeding lines you want, then print
that after you get your match followed by the matched line plus 7
successors.

        Ed.

 
 
 

"grep" several lines

Post by Robert Kat » Fri, 05 Dec 2003 03:06:30



> Hi,

> Often I need to find a section of a large file. I want to do something
> similar to grep but I also need e.g. the 5 lines proceeding the found line
> and the 7 lines after.

> OS is Solaris 8.

> How is that done the easiest way, eg. using sed or awk?

If the pattern can occur on one line only, then try

     #!/usr/bin/awk -f
                 { a[NR] = $0 }
     /pattern/   {
                     for (i = NR - 5; i <= NR; i++)
                         if (i > 0)
                             print a[i]
                     for (i = 0; i < 7; i++)
                         if (getline)
                             print $0
                 }

--
Regards,

---Robert