bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Grep: --quiet & suggestion


From: Stepan Kasal
Subject: Re: Grep: --quiet & suggestion
Date: Mon, 9 Jun 2003 11:50:49 +0200
User-agent: Mutt/1.2.5.1i

Hello,

On Sun, Jun 08, 2003 at 12:02:58PM +0300, Jori Mantysalo wrote:
> $ (grep -m 1 cat ; grep -m 1 dog) < text && echo 'Found!'
> cat
> dog
> Found!

well, even though this works on my system too, the folowing doesn't:

$ (echo cat;echo dog) | (grep -m 1 cat; grep -m 1 dog)
cat
$

In any way I wouldn'd use grep this way.

I tend to say that the bug is that ``grep'' doesn't close its input file
after the -m limit is reached.  When this bug is fixed, the above command
won't work in any case.

> An question: is there easy way to find text files containing first "cat"
> and then later "dog"? If you say
>  (grep -m 1 cat ; grep -m 1 dog) > /dev/null < text && echo 'Found!'
> it will work if text is cat-linefeed-dog, but it doesn't work if text is
> cat-space-dog.

Sorry, I think this usage is beyond the scope of grep.
You probably have to use a more programmable tool, like sed, awk, ruby or
perl.

Sed's language is a bit cryptic/terse.  This program could work:

#n
1,/cat/ {
        /cat.*dog/p
        d
}
/dog/p

or perhaps

/cat.*dog/p
1,/cat/d
/dog/!d

Ask in comp.unix.shell for more help.

awk has much more sane language:

BEGIN { cat_seen = 0 }
/cat.*dog/ { print "Found!"; exit }
!cat_seen {
        if ($0 ~ /cat/)
                cat_seen = 1
        next
}
/dog/ { print "Found!" }

or

!cat_seen && sub(/^.*cat/,"") { cat_seen = 1 }
/dog/ { print "Found!" }

more help in comp.lang.awk

The code is untested, sorry. HTH anyway.

In any case, I'm afraid that for more complex tasks one has to do some
kind of programming.  In fact, constructs like

        (grep -m 1 cat ; grep -m 1 dog) < text && echo 'Found!'

can already be called ``shell programs.''

Stepan Kasal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]