Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mork

macrumors 6502a
Original poster
Jan 9, 2009
539
34
I'm doing a regular expression, which works fine in RegExRx or any other RegEx tool I've tested, but when I try to do a GREP using that RegEx, it finds no files.

This RegEx finds all the files with a space on either side of the equals.
grep -Ri accesslevel = 3 dirname

--------------------------

But this RegEx, trying to account for possibly no or more than one space around the equals, finds nothing.
grep -Ri accesslevel\s*=\s*3 dirname

And, this similarly finds nothing:
grep -Ri accesslevel?*=?*3 dirname

What am I doing wrong?

Thanks in advance,

- M
 
Code:
grep -Ri 'accesslevel\s*=\s*3' dirname
My first guess is that you need to quote the pattern.

I'm surprised the first command-line you gave worked, because the spaces aren't quoted. It might work (in the sense of producing output) because everything after the word "accesslevel" is treated as a filename, and if there's no file or dir named "=" or "3", then it's simply skipped.

Quoting the pattern would look like this, for each example you gave:
Code:
grep -Ri 'accesslevel = 3' dirname

grep -Ri 'accesslevel\s*=\s*3' dirname

grep -Ri 'accesslevel?*=?*3' dirname

How to use the CODE tag in posts.


My second guess is that grep doesn't recognize "\s". You should read its man page to make sure, as this might vary depending on OS version (or post 'grep --version'). Also look at the 'egrep' command, and/or the -E option to grep.

For the grep I have here, it DOES NOT recognize "\s" as a meta pattern, which means it's treated as a simple "s" character.

The "*" means "zero or more times" for whatever the prior expression is. So on my machine, that would be "zero ore more S's". This would NOT match any space characters, because space isn't an "s" or "S".


The third command (with ?'s) is unlikely to produce what you want. If you expect "?" to mean "any character", then that's not how grep works. Grep's "any character" meta-character is "." (dot). Conversely, the shell's pattern expansion DOES treat ? as "any single character". Unfortunately, the shell's patterns are quite different from grep's.

Grep has named classes for a variety of character classes. The name for whitespace is "[:space:]". Since this name is only recognized between []'s, the pattern is "[[:space:]]". So the sub-pattern that means "zero or more whitespace" is:
Code:
grep -Ri 'accesslevel[[:space:]]*=[[:space:]]*3' dirname

"zero or more anything" is:
Code:
grep -Ri 'accesslevel.*=.*3' dirname

And "one or more whitespace" would be:
Code:
grep -Ri 'accesslevel[[:space:]]+=[[:space:]]+3' dirname


https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/egrep.1.html
https://developer.apple.com/legacy/...format.7.html#//apple_ref/doc/man/7/re_format
 
Last edited:
Code:
grep -Ri 'accesslevel\s*=\s*3' dirname
My first guess is that you need to quote the pattern.

I'm surprised the first command-line you gave worked, because the spaces aren't quoted. It might work (in the sense of producing output) because everything after the word "accesslevel" is treated as a filename, and if there's no file or dir named "=" or "3", then it's simply skipped.

Quoting the pattern would look like this, for each example you gave:
Code:
grep -Ri 'accesslevel = 3' dirname

grep -Ri 'accesslevel\s*=\s*3' dirname

grep -Ri 'accesslevel?*=?*3' dirname

How to use the CODE tag in posts.


My second guess is that grep doesn't recognize "\s". You should read its man page to make sure, as this might vary depending on OS version (or post 'grep --version'). Also look at the 'egrep' command, and/or the -E option to grep.

For the grep I have here, it DOES NOT recognize "\s" as a meta pattern, which means it's treated as a simple "s" character.

The "*" means "zero or more times" for whatever the prior expression is. So on my machine, that would be "zero ore more S's". This would NOT match any space characters, because space isn't an "s" or "S".


The third command (with ?'s) is unlikely to produce what you want. If you expect "?" to mean "any character", then that's not how grep works. Grep's "any character" meta-character is "." (dot). Conversely, the shell's pattern expansion DOES treat ? as "any single character". Unfortunately, the shell's patterns are quite different from grep's.

Grep has named classes for a variety of character classes. The name for whitespace is "[:space:]". Since this name is only recognized between []'s, the pattern is "[[:space:]]". So the sub-pattern that means "zero or more whitespace" is:
Code:
grep -Ri 'accesslevel[[:space:]]*=[[:space:]]*3' dirname

"zero or more anything" is:
Code:
grep -Ri 'accesslevel.*=.*3' dirname

And "one or more whitespace" would be:
Code:
grep -Ri 'accesslevel[[:space:]]+=[[:space:]]+3' dirname


https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/egrep.1.html
https://developer.apple.com/legacy/...format.7.html#//apple_ref/doc/man/7/re_format

First of all thank you for your excellent posting!

Unfortunately, not a single one of your suggestions worked and produced the same number of hits as simply saying:

grep -Ri accesslevel = 3 dirname

I'm stumped.

Unfortunately, there isn't AFAIK, a single search program for the Mac which supports Regular Expressions. Most, if not all of them, with a couple minor exceptions, lazily rely on the spotlight index which won't help me at all since these files are not known by Spotlight and will never have a SL extension.

Thanks very much.

- M
 
Please post the exact command-lines you used. Copy and paste them from the Terminal window. Use CODE tags.

Also post the output from the command-lines that produced output, again copy and paste from the Terminal window, and use CODE tags. If the output is more than 10 lines, post only the first 10 lines, and also post how many lines in total were output.

Finally, post the output of this:
Code:
grep --version

and also post exactly what your OS version is.
 
Grep Version: grep (BSD grep) 2.5.1-FreeBSD

OS Version: Mac High Sierra 10.13.2

Here's an expanded GREP that seems to work well:

$ grep -Ri -E 'accesslevel[[:space:]]*[>|<|=\>=|<=][[:space:]]*[3]' filedir

Thanks for the postings and suggestions! :)
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.