Bug in grep or man grep

Bug in grep or man grep

Post by Felix Schmid » Tue, 12 Oct 1999 04:00:00



Hi,

am i understanding something wrong, or is this a feature:

When i do a "grep -v" on a file that contains only one line, and this
line contains the pattern
i'm looking for, then grep returns 1, not 0 as it should when it matches
a pattern:

# echo line1 > file
# grep line1 file; echo $?
line1
0

BUT:
# grep -v line1 file; echo $?
1

When i add lines which are not matched by the expression, the return
code is zero:

# echo line2 >> file
# grep -v line1 file; echo $?
line2
0

It seems that -v implicitely alters (inverts) the expression,  not the
the output.
At least i would expect fgrep to behave the way i want, because it does
a string match instead of
a pattern match. However, it behaves the same.

In any case, the man page is wrong. It says that the return code is zero
if "any matches are found",
which is the case regardless whether the -v switch is set or not (from
my understanding).

Felix.

 
 
 

Bug in grep or man grep

Post by Kjetil Torgrim Homm » Tue, 12 Oct 1999 04:00:00


[Felix Schmidt]

Quote:>   When i do a "grep -v" on a file that contains only one line, and
>   this line contains the pattern i'm looking for, then grep returns
>   1, not 0 as it should when it matches a pattern:

Yes.  Another way of saying it is, grep returns 0 ("true") if there
was any output.  GNU grep behaves the same way.

Quote:>   In any case, the man page is wrong. It says that the return code
>   is zero if "any matches are found", which is the case regardless
>   whether the -v switch is set or not (from my understanding).

GNU grep's manual says:

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

Solaris' manual says:

     -v        Print all lines except those that contain the pat-
               tern.

I think GNU's description is clearer.

Kjetil T.

 
 
 

1. grep sTerm * | grep -v grep ???

in .bashrc

alias r='ps ax | grep $1 | grep -v grep'

this works from the command line but doesn't work from the bashrc.
Apparently there's a syntax problem.

...and

. filename

where can I read about this commad that is found in bash and sh
scripts?  I searched the bash and one or two other man pgs to no
avail.  Thanks gurus.

--


2. httpd not starting: cannot determine local host name

3. tail | grep | grep | grep

4. unixbench result for kernel 2.5.60.

5. How to make grep select both lines using only a single 'grep' command

6. changing process title

7. grep question New to grep

8. Apache 1.3 doesn't authenticate in frames?

9. grep octal value (grep '\0145' /tmp/test) doesn't work !!

10. lscfg | grep en and then grep lo

11. PLEASE HELP A NEWBIE: grep doesn't grep

12. grep attempts to grep in non-ascii files

13. Using grep -v and grep -C together