LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   find | grep <pattern> not working (http://www.linuxquestions.org/questions/linux-software-2/find-%7C-grep-pattern-not-working-481609/)

duvalr 09-08-2006 11:30 AM

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

MensaWater 09-08-2006 11:44 AM

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.

matthewg42 09-08-2006 12:52 PM

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.

duvalr 09-09-2006 07:31 PM

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

matthewg42 09-17-2006 05:07 AM

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.


All times are GMT -5. The time now is 12:24 PM.