LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-08-2006, 10:30 AM   #1
duvalr
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
find | grep <pattern> not working


hi,
grep is failing to display all that it should.

I'm running this Linux:
Red Hat Enterprise Linux AS release 3 (Taroon Update 6) Kernel 2.4.21-37.EL on an i686

I have these 3 files:
[root]# ls /proc/scsi/qla2300
2 3 HbaApiNode
[root]# ll /proc/scsi/qla2300 total 0
-rw-r--r-- 1 root root 0 Sep 7 20:53 2
-rw-r--r-- 1 root root 0 Sep 7 20:53 3
crw------- 1 root root 254, 0 Aug 31 10:52 HbaApiNode

This is the script commands that fail:
[root]# find /proc/scsi/qla2300 2>/dev/null | grep /[0-9] 2>/dev/ null
/proc/scsi/qla2300/2

It should return 2 lines of output:
/proc/scsi/qla2300/3
/proc/scsi/qla2300/2

These are my find and grep versions:
[root]# rpm -qf `which find`
findutils-4.1.7-9
[root]# rpm -qf `which grep`
grep-2.5.1-24.5

If I test this on other Linux (SUSE and RH (not AS)) then all is well, displays both as it should.

So, is this a bug in Red Hat AS grep?
Is there an updated version that may work?

thanks,
roger
 
Old 09-08-2006, 10:44 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,006
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
On my RH AS 3 (Update 2) it works fine.

I have the same findutils version as you.

My grep is an earlier version thann yours: grep-2.5.1-16

Maybe you have a hidden character in your command line or in your file name?

Try retyping the command line instead of getting it from history.

Try typing "ls -l /proc/scsi/qla2300/2" then "ls -l /proc/scsi/qla2300/3" to be sure ls sees the file by the exact name. (I've seen dozens of times where a special character gets hidden in a file's name so a directory listing shows it but specifically listing the file by the name I see doesn't work.)

You might also try: find /proc/scsi/qla2300 2>/dev/null | grep \/[0-9] 2>/dev/ null

Just to be sure it doesn't treat the forward slash as anything other than a literal.

Also trying:
find /proc/scsi/qla2300 2>/dev/null | egrep "/2|/3" 2>/dev/ null might work.
 
Old 09-08-2006, 11:52 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by duvalr
hi,
This is the script commands that fail:
[root]# find /proc/scsi/qla2300 2>/dev/null | grep /[0-9] 2>/dev/ null
/proc/scsi/qla2300/2

It should return 2 lines of output:
/proc/scsi/qla2300/3
/proc/scsi/qla2300/2
You should try (redirects removed for clarity):

Code:
# find /proc/scsi/qla2300 | grep "/[0-9]"
The difference is the quoting of the argument to grep. I suspect you have a file or directory in your root directory with the name "2". So when the shell evaluates /[0-9], it expands this to the list of files which match the pattern /[0-9], in this case /2. grep is therefore instructed to grep for /2, which only matches the one line in the output of find.

By quoting the pattern to grep you stop the shell evaluating the pattern before it passes the argument to grep. I think it's generally good practise to quote arguments to grep for this reason.
 
Old 09-09-2006, 06:31 PM   #4
duvalr
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Yes - "/2" file present causing this, but why?

Thanks so much jlightner and matthewg42 for your quick replies. I have been trying to solve this for days now. I am very green to Linux/Unix, came from the OpenVMS world:

jlightner,
No hidden chars, retyped many times without using command recall.
Yes both files are seen:
[root]# ls -l /proc/scsi/qla2300/2
-rw-r--r-- 1 root root 0 Sep 10 07:16 /proc/scsi/qla2300/2
[root]# ls -l /proc/scsi/qla2300/3
-rw-r--r-- 1 root root 0 Sep 10 07:16 /proc/scsi/qla2300/3

and this produces same result:
[root]# find /proc/scsi/qla2300 2>/dev/null | grep \/[0-9] 2>/dev/null
/proc/scsi/qla2300/2
while this is expected results:
[root]# find /proc/scsi/qla2300 2>/dev/null | egrep "/2|/3" 2>/de
v/null
/proc/scsi/qla2300/3
/proc/scsi/qla2300/2

matthewg42,
Yes. The quotes do work, I did read that quotes around grep patterns was good practice.
[root]# find /proc/scsi/qla2300 | grep "/[0-9]"
/proc/scsi/qla2300/3
/proc/scsi/qla2300/2

And yes I do in fact have a file named "2" in the root:
[root]# ls -l /2
-rw-r--r-- 1 root root 0 Sep 7 19:28 /2
[root]#

So if I go to my SUSE Linux and add a "/2" file then I will get the same false result, right? And if jlightner does same he will also?

[root]# find /proc/scsi/qla2300 | grep -v /[0-9]
/proc/scsi/qla2300
/proc/scsi/qla2300/3
/proc/scsi/qla2300/HbaApiNode
[root]# find /proc/scsi/qla2300 | grep -c /[0-9]
1
[root]#

Why does the "-v" on the grep see the "3" file?
And on the "-c" it's really counting the "/2" at the root? Then why does it display "/proc/scsi/qla2300/2"?

So, bottom line is "USE QUOTES"! Why does grep not force you to, give a syntax error when no quotes? If you can get 2 different results based on having a file with the same name at the root or not then that is a problem as i see it.

thanks again,
roger

Last edited by duvalr; 09-10-2006 at 06:15 PM.
 
Old 09-17-2006, 04:07 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by duvalr
So if I go to my SUSE Linux and add a "/2" file then I will get the same false result, right? And if jlightner does same he will also?
Sorry for the dela in this reply - just got back from a week in Rhodes (lovely place btw.)

Yes, if you have a /2 file on an unix like system you will get the same thing, at least if youo are using bash as your shell (I'm not sure if all shells will expand regular expression filename patterns - some may only expand simple globbing - the * and ? special characters).

Quote:
[root]# find /proc/scsi/qla2300 | grep -v /[0-9]
/proc/scsi/qla2300
/proc/scsi/qla2300/3
/proc/scsi/qla2300/HbaApiNode
[root]# find /proc/scsi/qla2300 | grep -c /[0-9]
1
[root]#

Why does the "-v" on the grep see the "3" file?
Because after the shell expands the regular expression pattern, the command is:

Code:
# find /proc/scsi/qla2300 | grep -v /2
The -v switch says exclude lines containing /2, so /proc/scsi/qla2300/2 is not shown, but all others are.

Quote:
And on the "-c" it's really counting the "/2" at the root? Then why does it display "/proc/scsi/qla2300/2"?
I'm not sure which command made this happen.

Quote:
So, bottom line is "USE QUOTES"! Why does grep not force you to, give a syntax error when no quotes? If you can get 2 different results based on having a file with the same name at the root or not then that is a problem as i see it.
Yeah, it's generally a good idea. grep doesn't force anything, or even see the quotes. The shell interprets filename patterns before it passes the arguments to grep, so it's up to whoever is calling grep know to quote patterns which he shell might interpret so as to be sure that grep gets the proper arguments.

Incidentally, this insight is one of the items which I think is among the main "aha" moments of using the shell - knowing that the shell expands some sorts of patterns before it passes them to programs.
 
  


Reply

Tags
grep


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
how to find a pattern using shell script sharad Linux - General 1 05-23-2006 03:50 AM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 08:30 PM
pattern search through find command. abhi Linux - Newbie 2 04-11-2005 10:31 AM
using grep when the pattern contains a ! farmerjoe Programming 9 03-15-2005 11:04 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration