LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-21-2011, 11:30 PM   #1
MrDetail
LQ Newbie
 
Registered: Feb 2011
Distribution: ubuntu
Posts: 11

Rep: Reputation: 0
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
 
Old 02-22-2011, 12:08 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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?
 
Old 02-22-2011, 12:12 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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]'
 
Old 02-22-2011, 12:18 AM   #4
MrDetail
LQ Newbie
 
Registered: Feb 2011
Distribution: ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
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.
 
Old 02-22-2011, 12:23 AM   #5
MrDetail
LQ Newbie
 
Registered: Feb 2011
Distribution: ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
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
 
Old 02-22-2011, 12:30 AM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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'
 
Old 02-22-2011, 12:37 AM   #7
kingston
Member
 
Registered: Mar 2008
Location: Bengaluru, India
Distribution: RHEL 5.5, Solaris 5.10
Posts: 215
Blog Entries: 1

Rep: Reputation: 21
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
 
Old 02-22-2011, 12:42 AM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.
 
Old 02-22-2011, 12:44 AM   #9
MrDetail
LQ Newbie
 
Registered: Feb 2011
Distribution: ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 02-22-2011, 12:48 AM   #10
MrDetail
LQ Newbie
 
Registered: Feb 2011
Distribution: ubuntu
Posts: 11

Original Poster
Rep: Reputation: 0
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 ~:$
 
Old 02-22-2011, 12:56 AM   #11
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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...

Last edited by Dark_Helmet; 02-22-2011 at 01:09 AM. Reason: tag typo & spelling
 
Old 02-22-2011, 12:57 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Try echo $GREP_OPTIONS
 
Old 02-22-2011, 01:07 AM   #13
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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.

Last edited by Dark_Helmet; 02-22-2011 at 01:36 AM.
 
1 members found this post helpful.
Old 02-22-2011, 02:06 AM   #14
kurumi
Member
 
Registered: Apr 2010
Posts: 228

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep lines before and after magnum81 Programming 3 07-29-2011 05:52 PM
grep command returns a large value in RH5 U2 cui_jenny001 Linux - Software 14 05-22-2009 06:08 PM
grep: Unmatched [ or [^ sank1800 Linux - Newbie 6 04-29-2009 09:39 AM
Getting only unique lines using grep.. sph90457 Linux - Newbie 6 07-05-2007 09:31 AM
Grep lines gsibble Linux - General 1 04-12-2004 02:30 AM


All times are GMT -5. The time now is 01:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration