LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-05-2012, 02:11 PM   #1
davemha
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Rep: Reputation: Disabled
Unhappy Grep broken?


Here is the command and its output:

# echo 401 | grep [0-9]
401



Normal, right? Now look what happens when I change the number:

# echo 420 | grep [0-9]
#

What's up with that????

Same for any line that ends in 0:
# echo 4400 | grep [0-9]
# echo 440223453243430 | grep [0-9]
# echo 990223453 243430 | grep [0-9]

Put something else as the final character:
# echo 440223453 243430t | grep [0-9]
440223453 243430t

And grep lets it through. I have several systems, this is the only one on which I've seen this behaviour.

# grep -V
GNU grep 2.6.3

# cat /etc/redhat-release
CentOS Linux release 6.0 (Final)
 
Old 06-05-2012, 02:17 PM   #2
davemha
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Update:
Now I'm not sure what the failure pattern is. When I run:
echo 440x | grep [0-9]

it returns nothing also. This can't be a bug, it's too huge. I must be missing something.
 
Old 06-05-2012, 02:34 PM   #3
davemha
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Question What the....

Log out, log back in, works normally now.

I went back through every command I ran in the session where it was screwed up. I can't see anything that would have any bearing on how grep, echo, or bash in general operates.

No clue. But it's working now. For now.
 
Old 06-05-2012, 02:39 PM   #4
davemha
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Final update, probably

A friend of mine says, "your shell is interpreting the [] characters before sending them to grep,
quote your regex strings."

Unfortunately I can't test to see if he's right, since it's working now and I have no idea how to reproduce the problem.
 
Old 06-05-2012, 11:04 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
The only thing worse than not knowing the reason for a problem is not knowing how it got fixed.......

I don't understand why you can't test what you've learned---take your original example and repeat using quotes.
 
Old 06-06-2012, 01:43 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,809

Rep: Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870
in such cases you should almost never think grep is broken (or sed or whatever). If grep was broken so clearly it would be fixed already. No, that is not a bug in the software, but misunderstanding of the environment. Believe me, grep (sed/awk/...) works exactly as planned and implemented. So you need to find out what can cause this behavior.
An easy trick to execute echo grep [0-9] (or simply echo [0-9]) and you will see it is evaluated. To keep it as is you need to avoid evaluation, you need to look for quotations (as it was already mentioned).





_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 06-06-2012, 11:21 AM   #7
davemha
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
in such cases you should almost never think grep is broken
I know - dumb thought. It was just so weird.


Quote:
I don't understand why you can't test what you've learned---take your original example and repeat using quotes.
I can't test the solution if I can't reproduce the problem. Trying the solution in an environment that isn't messed up doesn't yield any useful results:

# echo 420 | grep [0-9]
420
# echo 420 | grep "[0-9]"
420

# echo grep [0-9]
grep [0-9]
# echo grep "[0-9]"
grep [0-9]
 
Old 06-06-2012, 11:30 AM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,429

Rep: Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029Reputation: 2029
The results will depend on whether any files with names that are just a single digit exist in the current directory. Try your "echo" test when there is a file named "3" (without the quotes) in the current directory. Try it again when there are files named "3", "4", and "5", and consider what grep would do in that case.
 
1 members found this post helpful.
Old 06-06-2012, 12:01 PM   #9
davemha
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Too cool. So the shell IS interpreting the regex before it gets to grep.

# echo 420 | grep [0-9]
420
# touch 0; echo 420 | grep [0-9]
420
# mv 0 1; echo 420 | grep [0-9]
# mv 1 2; echo 420 | grep [0-9]
420
# mv 2 3; echo 420 | grep [0-9]
# mv 3 4; echo 420 | grep [0-9]
420
# mv 4 5; echo 420 | grep [0-9]
# mv 5 6; echo 420 | grep [0-9]
# mv 6 7; echo 420 | grep [0-9]
# mv 7 8; echo 420 | grep [0-9]
# mv 8 9; echo 420 | grep [0-9]
# rm -f 9; echo 420 | grep [0-9]
420


And, if I use quotes (grep "[0-9]") it works in all cases. Very cool. I like learning new linux stuff.
 
Old 09-23-2014, 12:59 PM   #10
cki
LQ Newbie
 
Registered: Sep 2014
Posts: 1

Rep: Reputation: Disabled
On the topic of grep behaving strangely, beware of invisible characters like the Windows style newline characters.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep has no effect - does not grep anything in this for loopa LinuxChiq Linux - Newbie 2 12-01-2011 09:03 PM
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 02:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:46 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
Open Source Consulting | Domain Registration