bug-grep
[Top][All Lists]
Advanced

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

bug#69929: Can grep -q report matches in incomplete lines?


From: Niels Möller
Subject: bug#69929: Can grep -q report matches in incomplete lines?
Date: Fri, 22 Mar 2024 09:24:24 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Paul Eggert <eggert@cs.ucla.edu> writes:

> On 3/21/24 06:57, Niels Möller wrote:
>> I'm having grep -q read input from a pipe. I would like grep to exit
>> successfully as soon as a match occurs, without requiring the line to be
>> terminated by newline or EOF (unless the grep pattern includes '$', that
>> is).
>
> Grep used to behave almost that way. It did wait for a newline but it
> did not wait for EOF. But people started complaining about that
> behavior so we changed it.

Some --exit-early option? Sounds reasonable, but not enough for my
usecase. And I understand it might be an undesirable default behavior of
-q: Not so nice if changing "foo | grep" to "foo | grep -q" could make
foo fail with EPIPE/SIGPIPE.

(BTW, man page says about the -q option "Exit immediately with zero
status if any match is found", from reading that, I would not expect
grep to continue to read the rest of the file until EOF).

> However, not waiting for a newline is problematic; although doable it
> would be a bit of a pain to program and not sure it's worth the
> effort. Grep didn't used to do that.

I only have a rather abstract understanding of regexp matching, but for
patterns that aren't extended to require backtracking, in principle it
should be doably to feed the state machine one character at a time, with
no buffering of input?

jackson@fastmail.com writes:

> If you're not sure whether your actual input of interest will end
> in a newline, can you add one, to "feed grep's newline hunger",
> thus for instance replacing your example:
>
>   grep -q foo <(sh -c 'printf foo ; sleep 30' &)
>
> with:
>
>   grep -q foo <(sh -c 'printf foo ; echo ; sleep 30' &)

Not so easy when the input is the log output by a still running process
(in my case qemu).

> In any case, grep is quite line oriented,

Right. Maybe what I'm really looking for is a binary grep. For the time
being, I hacked together a tool that does just what I need for my qemu
test (no regexp matching, just matching a fix string starting at the
beginning of a line). See
https://git.glasklar.is/system-transparency/core/stboot/-/blob/43e91273a57473ee361e332926b88c1b4fe9917a/integration/look-for/look-for.go

I use this in combination with timeout (GNU coreutils) to fail the test
if no match for the login prompt is found within reasonable time.

A more general binary grep could match a regexp against a binary file or
stream, with options to exit on first match (my immediate usecase),
output the count of matches, or output the positions for each match. I
still think some of those features would make sense as part of GNU grep.

Regards,
/Niels

-- 
Niels Möller. PGP key CB4962D070D77D7FCB8BA36271D8F1FF368C6677.
Internet email is subject to wholesale government surveillance.





reply via email to

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