Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there something like a "wide" grep ?

0 views
Skip to first unread message

bartw...@my-deja.com

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
Hello,

I was wondering if anyone knows of a command that can grep a file for a
certain keyword ( that would be grep, right ? ) , but instead of just
displaying the line with the keyword, display a number of lines before
and after that specific line.

What I want to do is grep a file for e.g. "error" and then have it
display to me the whole set of lines concerning the error.
IF for example you get a disk error , only one line has the error
keyword, but the interesting data is in the lines following ...

Thanks for any help or hints in advance,

Bart


Sent via Deja.com http://www.deja.com/
Before you buy.

Steve Middleton

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to

<bartw...@my-deja.com> wrote in message
news:8jutbr$rnm$1...@nnrp1.deja.com...

> Hello,
>
> I was wondering if anyone knows of a command that can grep a file for a
> certain keyword ( that would be grep, right ? ) , but instead of just
> displaying the line with the keyword, display a number of lines before
> and after that specific line.
>
> What I want to do is grep a file for e.g. "error" and then have it
> display to me the whole set of lines concerning the error.
> IF for example you get a disk error , only one line has the error
> keyword, but the interesting data is in the lines following ...
>

Bart, I would suggest you look into AWK, it is ideal for pattern matching
and performing
conditional operations on files .... failling that you could grep -n to get
the line number and
then head/tail different parts of the file working with that number to get
the relevant lines
although that could turn a little messy!

rgds
steve

bartw...@my-deja.com

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
Thanks Steve,

but I think I am going to jump to Perl if scripting gets involved. A
standard tool would be nicer of course , since I would get this on any
machine I lay me hands on ... might be a suggestion for Sun to include
on their freeware CD ?!


Cheers ,

Bart


In article <962802401.23281.0...@news.demon.co.uk>,

Barry Margolin

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
In article <8jutbr$rnm$1...@nnrp1.deja.com>, <bartw...@my-deja.com> wrote:
>Hello,
>
>I was wondering if anyone knows of a command that can grep a file for a
>certain keyword ( that would be grep, right ? ) , but instead of just
>displaying the line with the keyword, display a number of lines before
>and after that specific line.

GNU grep can do this, with its -A, -B, and -<number> options.

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

Rich Teer

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
On Wed, 5 Jul 2000 bartw...@my-deja.com wrote:

> Hello,
>
> I was wondering if anyone knows of a command that can grep a file for a
> certain keyword ( that would be grep, right ? ) , but instead of just
> displaying the line with the keyword, display a number of lines before
> and after that specific line.

I think GNU's grep does what you're after.

--
Rich Teer

NT tries to do almost everything UNIX does, but fails - miserably.

The use of Windoze cripples the mind; its use should, therefore, be
regarded as a criminal offence. (With apologies to Edsger W. Dijkstra)

Voice: +1 (250) 979-1638
URL: http://www.rite-online.net


Philip Guenther

unread,
Jul 5, 2000, 3:00:00 AM7/5/00
to
bartw...@my-deja.com writes:

> I was wondering if anyone knows of a command that can grep a file for a
> certain keyword ( that would be grep, right ? ) , but instead of just
> displaying the line with the keyword, display a number of lines before
> and after that specific line.
>

> What I want to do is grep a file for e.g. "error" and then have it
> display to me the whole set of lines concerning the error.
> IF for example you get a disk error , only one line has the error
> keyword, but the interesting data is in the lines following ...

Sounds like you should check out GNU grep, downloadable from
ftp://ftp.gnu.org/pub/gnu/grep/

It supports the -C (context), -A (after) and -B (before) options, all of
which take a integer to specify how many lines to show. So if you want
to see all occurances of the regexp "blah" in a file with their
following 3 lines you would use the command

grep -A3 "blah" file


Philip Guenther

----------------------------------------------------------------------
guen...@gac.edu UNIX Systems and Network Administrator
Gustavus Adolphus College St. Peter, MN 56082-1498
Source code never lies: it just misleads (Programming by Purloined Letter?)

Mitch Richling

unread,
Jul 7, 2000, 3:00:00 AM7/7/00
to
On Wed, 5 Jul 2000, Steve Middleton wrote:

> Date: Wed, 5 Jul 2000 14:55:55 +0200
> From: Steve Middleton <st...@dataload.co.uk>
> Newsgroups: comp.sys.sun.admin
> Subject: Re: Is there something like a "wide" grep ?


>
>
> <bartw...@my-deja.com> wrote in message
> news:8jutbr$rnm$1...@nnrp1.deja.com...
> > Hello,
> >

> > I was wondering if anyone knows of a command that can grep a file for a
> > certain keyword ( that would be grep, right ? ) , but instead of just
> > displaying the line with the keyword, display a number of lines before
> > and after that specific line.
> >
> > What I want to do is grep a file for e.g. "error" and then have it
> > display to me the whole set of lines concerning the error.
> > IF for example you get a disk error , only one line has the error
> > keyword, but the interesting data is in the lines following ...

GNU Grep works. The -A and -B options. The grep included with Solaris
is a bit weak.

-Mitch Richling

=== Mitch J. Richling ====== http://www2.airmail.net/richmit/index.html ==
| Texas Instruments Inc. | Office: K1-14444 | Home: 972-301-3148 |
| 12500 TI Blvd. MS#8717 | Phone: 214-480-4435 | Cell: 214-497-1105 |
| Dallas ASP Design Computing | Pager: 214-961-0935 | Wife: 214-497-1103 |
| Dallas, TX 75243 | Fax: 214-408-2700 | |
==== ric...@dal.asp.ti.com ===== ric...@member.ams.org =================

Quote: "1+1=2" -o)
-- A.N. Whitehead & B. Russell, /\\
Principia Mathematica, Vol. II, p. 83. _\_v

0 new messages