LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep [A-Z] returns unmatched lines (https://www.linuxquestions.org/questions/linux-newbie-8/grep-%5Ba-z%5D-returns-unmatched-lines-864212/)

MrDetail 02-21-2011 10:30 PM

grep [A-Z] returns unmatched lines
 
Hello new linux user (2 months) first question

does anyone know why something like:
echo lowercase | grep [A-Z]
would still return the lowercase line even tho there is no match?
Using --color will show there is no match but the line is still returned.
I am still new but from what i've learned this should not work like that.
ubuntu 10.10
Thanks in advance

Dark_Helmet 02-21-2011 11:08 PM

It doesn't return a match on my system:
Code:

$ echo lowercase | grep [A-Z]
$ bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ grep --version
GNU grep 2.5.4

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$

Could you copy-paste your command and results directly from the shell?

catkin 02-21-2011 11:12 PM

Perhaps [A-Z] is being expanded by the shell as illustrated in this command prompt session:
Code:

c@CW8:/tmp$ touch A
c@CW8:/tmp$ echo grep [A-Z]
grep A

Solution: quote the grep argument to prevent the shell expanding it: grep '[A-Z]'

MrDetail 02-21-2011 11:18 PM

22:11:28 ~:$ echo lowercase | grep [A-Z]
lowercase
22:11:53 ~:$ echo UPpercase | grep [A-Z]
UPpercase
22:12:22 ~:$ bash --version
GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
22:12:41 ~:$ grep --version
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


22:16:07 ~:$ echo UPpercase | grep --color [A-Z]
UPpercase

The "UP" in UPpercasse was returned red.

MrDetail 02-21-2011 11:23 PM

Quote:

Originally Posted by catkin (Post 4266869)
Solution: quote the grep argument to prevent the shell expanding it: grep '[A-Z]'

22:17:45 ~:$ echo lowercase | grep '[A-Z]'
lowercase

no dice i guess my grep is just stupid

Dark_Helmet 02-21-2011 11:30 PM

Not necessarily :)

Do you have any aliases that could be interfering?
Code:

$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ls='ls --color=auto'


kingston 02-21-2011 11:37 PM

In my case it works fine. But i am using a different bash version running on RHEL 5.4

[xkinved@einbalx ~]$ echo lowercase |grep '[A-Z]'
[xkinved@einbalx ~]$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
[xkinved@einbalx ~]$echo LOwercase |grep '[A-Z]'
LOwercase

Dark_Helmet 02-21-2011 11:42 PM

Also, one other thing you can try: tell bash to tell you what it's doing :)
Code:

$ set -x
$ echo lowercase | grep [A-Z]
+ echo lowercase
+ grep --color=auto '[A-Z]'
$

It won't tell you what grep itself is doing, but it will make sure there are no problems in-between you pressing the "Enter" key and grep taking control.

You turn off the extra info from bash by executing "set +x" when you're done.

MrDetail 02-21-2011 11:44 PM

Code:

alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

That is all of them i cant imagine that the color would cause its behaviour

MrDetail 02-21-2011 11:48 PM

Code:

alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

That is all of them i cant imagine that the color would cause its behaviour

Code:

22:39:38 ~:$ set -x
22:45:24 ~:$ echo lowercase | grep [A-Z]
+ echo lowercase
+ grep --color=auto '[A-Z]'
lowercase
22:46:02 ~:$


Dark_Helmet 02-21-2011 11:56 PM

I have to say that's a bit bizarre. I did notice that 2.6.3 was on the tail end of a rapid-fire release session as indicated in the release notes here.

What I'll do is download the source for 2.6.3, compile, and run it to see if I get the same result as you.

There was no 2.6.4 release, and the 2.7 release (5 months after 2.6.3) doesn't seem to mention a bug fix that would explain this (here).

Off to compile and test...

catkin 02-21-2011 11:57 PM

Try echo $GREP_OPTIONS

Dark_Helmet 02-22-2011 12:07 AM

After compiling, I can confirm that 2.6.3 is spitting out a bad result.

Code:

$ mkdir temp_grep
$ cd temp_grep
$ wget http://mirrors.usc.edu/pub/gnu/grep/grep-2.6.3.tar.gz

<snip>
$ tar xvzf grep-2.6.3.tar.gz
<snip>
$ mkdir grep_build
$ cd grep_build
$ ../grep-2.6.3/configure
<snip - no errors>
$ make
<snip - no errors>
$ echo lowercase | grep [A-Z]
$ echo lowercase | src/grep [A-Z]
lowercase
$

EDIT:
And as a follow-up, I downloaded the 2.7 source, compiled and ran. 2.7 gives the expected output (i.e. nothing). So it seems there is a bug in 2.6.3, but that bug appears to be fixed in 2.7.

kurumi 02-22-2011 01:06 AM

Sometimes it has to do with your locale. Try a different locale like C, and see.


All times are GMT -5. The time now is 08:44 PM.