[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Off topic question about grep



On 11/09/2010 06:00 AM, Jochen Schulz wrote:
> ~Stack~:
>>
>> But that would match against 9_asD which begins with a number (not what
>> I wanted). So I tried:
>> [_a-zA-Z][_a-zA-Z0-9]*
>>
>> I realize that the expression won't do what I mistakenly thought I
>> wanted it to do. What is puzzling to me is that my hard disk usage
>> peaked, my cpu jumped, and grep took almost two minutes to return an
>> exit code of 1 (no match). :-/
> 
> What was your exact command line? Did you quote the regular expression?
> My guess is that the shell interpreted the '*' character for you and you
> ended up with a command line like this:
> 
> $ grep [_a-zA-Z][_a-zA-Z0-9]file1 file2 file3
> 
> where file1 etc. are the files in your current directory. That's why
> grep took so long to finish and it didn't find anything because file1 is
> part of your regexp.
> 
> J.

To be pedantically correct ;)

grep [_a-zA-Z][_a-zA-Z0-9]*

The shell will expand the above into space separated values, based on
matches to the glob pattern.  The first match will become the pattern
used by grep, searched for in the remaining file names.  Try this:

  echo grep [_a-zA-Z][_a-zA-Z0-9]*

to see what the shell does in any particular case.  For example, I got:

  grep 00firefox-files_before 01cache.list ... xxyy

The ... in the above is 57 files.  58 files counting the xxyy were
searched for the "pattern" 00firefox-files_before, which is actually a
file name and so not likely to be found in any of the files searched.
If you want to prove this, try:

  ls > files
  grep *
  files:00firefox-files_before

-- 
Bob McGowan


Reply to: