find | grep <pattern> not working
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
It should return 2 lines of output:
These are my find and grep versions:
[root]# rpm -qf `which find`
[root]# rpm -qf `which grep`
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?
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.
find /proc/scsi/qla2300 2>/dev/null | egrep "/2|/3" 2>/dev/ null might work.
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.
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:
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
while this is expected results:
[root]# find /proc/scsi/qla2300 2>/dev/null | egrep "/2|/3" 2>/de
Yes. The quotes do work, I did read that quotes around grep patterns was good practice.
[root]# find /proc/scsi/qla2300 | grep "/[0-9]"
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
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]
[root]# find /proc/scsi/qla2300 | grep -c /[0-9]
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.
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).
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 10:25 AM.|