bug-grep
[Top][All Lists]
Advanced

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

Re: Bug#454690: "grep -v" alway returns 0


From: Wacek Kusnierczyk
Subject: Re: Bug#454690: "grep -v" alway returns 0
Date: Sun, 29 Mar 2009 18:51:43 +0200
User-agent: Thunderbird 2.0.0.21 (X11/20090318)

hello,

maybe i can help a little.

Eduard Bloch wrote:
> * Aníbal Monsalve Salazar [Sun, Mar 29 2009, 02:59:37PM]:
>   
>>
>> I can reproduce this bug.
>>
>> grep -V
>> GNU grep 2.5.4
>>
>> echo -e 'hola\nhello' | grep -v hola; echo $?
>> hello
>> 0
>>     

yes;  one line does *not* match the pattern and is thus *selected* (for
output) because of the inversion -- hence the *success* code.

>> echo -e 'hola\nhello' | grep -v hallo; echo $?
>> hola
>> hello
>> 0
>>     


yes; two lines do *not* match the pattern and are thus *selected*,
because of the inversion -- hence the *success* code.

where's 'this' bug?



> And what's the point of your test?
>
> $ echo -e 'blub\nbla'  | grep -v blub ; echo $?
> bla
> 0
>   

indeed.

> $ echo -e 'blub'  | grep -v blub ; echo $?
> 1
>   

indeed;  all lines match, there are *no non-matching* lines to be
selected -- hence the failure code.

> $ echo -e 'bla\nblub'  | grep -v blub ; echo $?
> bla
> 0
>   

indeed.

> What's the meaning of the exit status? Manpage talks about "selected
> lines". 

it says:

"Normally,  the  exit  status  is  0  if  selected lines are found and 1
otherwise."


> What does this mean with -v? 

it means that if selected lines are found, the code is 0, and 1
otherwise (modulo error cases).

> What does "selection" mean? 

the man page says:

"       -v, --invert-match
              Invert the sense of matching, to select non-matching lines."

'select non-matching lines' means that selected lines are those that do
not match the pattern.

> The -v
> pattern is matched, is this "selected"? 

if the pattern is matched *and* -v is specified, the line is *not*
printed, i.e., it is *not* selected.

> Intuitive answer: yes. 

wrong intuition, i'd say.

> But it
> seems that "selected" refers to the rest, i.e. NOT matched lines in
> this case.
>   

yepp.

> The problem is, if you don't know for sure, you have to try it out
> untill you hope to know. 

agree, this could be a little bit more clear.  there are some other
issues with grep and its man page .

> And even than it can be misleading:
>
> $ echo -n | grep -vq bla ; echo $?
> 1
> $ echo -e | grep -vq bla ; echo $?
> 0
>   

well, what's misleading here?  in the first, there's no line, nothing is
selected, hence the failure code.  in the second, there's one
non-matching line, it's selected (because of -v), hence the success
code.  that you suppress the output is another story -- lines may be
*selected* and yet not printed.

btw., the first above is interesting in that

    echo -n | grep -q 'foo'; echo $?
    # 1
    echo -n | grep -vq 'foo'; echo $?
    # 1

and that's precisely because there are no lines to be matched, hence
grep fails to select any line in both cases.

does this help?  the lesson:  be careful not to cry 'bug!' too eagerly. 
in this case, it seems to be a minor issue with the documentation (and
quite arguably so).

vQ







reply via email to

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