LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regular expressions not working for grep?? (https://www.linuxquestions.org/questions/linux-newbie-8/regular-expressions-not-working-for-grep-4175429949/)

sethbw 10-01-2012 04:05 PM

Regular expressions not working for grep??
 
Hello guys,

I'm trying to locate any template files in our file hierarchy that contain specific patterns... I'm familiar with regex... have been using it for a few years now with the assistance of regexr...

I am using Ubuntu 11.04

Now when I try using the syntax found on the man pages and many other how-to sites I never get any results, yet I know there are files with these patterns in them...

What the deal, yo?

Examples of patterns I'm using that simply do not return any results:

grep -r "radius\(" *
grep -r "maximum\-scale\=1" *
grep -r "\d+em\;" *


Any help provided on this matter is greatly appreciated.

Cheers,
S

Didier Spaier 10-01-2012 04:31 PM

From where did you issue the grep command? if not from the root of the tree you want to search in, try: "grep -r <pattern> /root/of/the/tree/*"

ntubski 10-01-2012 05:56 PM

Quote:

I'm familiar with regex... have been using it for a few years now with the assistance of regexr...
Different regex libraries often use slightly different syntaxes for the special meta-characters.

man grep(1):
Quote:

Basic vs Extended Regular Expressions

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).
Also, use single quotes unless you need variable expansion:
Code:

grep -r "radius\(" *
grep -r 'radius(' *
grep -r "maximum\-scale\=1" *
grep -r 'maximum-scale=1' *
grep -r "\d+em\;" *
grep -r '[0-9]\+em;' *


sethbw 10-02-2012 09:45 AM

Hmmm isn't this example:

Quote:

grep -r 'radius(' *

the exact opposite of what the man page says below?
Quote:

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

I did read the man page before posting here btw... it doesn't seem to make any sense.

When I try with the backslash in front of the parenthesis this is the return:

Quote:

$ grep -r 'radius\(' *
grep: Unmatched ( or \(
I am at the root where many files have multiple lines of CSS3 border-radius styles containing the exact spelling and capitalization as: "-radius(" -In fact I can look them up and open them in the folder tree using vi or nano... however this returns 0 results:

Quote:

$ grep -r 'radius(' *

SecretCode 10-02-2012 01:52 PM

Code:

grep -r 'radius(' *
works for me with a test file.

Do you get a match with
Code:

grep 'radius' aknownfile
grep -r 'radius' *
grep 'radius(' aknownfile

etc?

ntubski 10-02-2012 02:44 PM

Quote:

Originally Posted by sethbw (Post 4794973)
Hmmm isn't this example:
Quote:

grep -r 'radius(' *
the exact opposite of what the man page says below?

I did read the man page before posting here btw... it doesn't seem to make any sense.

The man page is very terse, but what it's saying is that in the default basic mode, a '(' will match a literal '(' in the text. You use '\(' for grouping, eg: 'Linux\(Questions\|Answers\)' matches 'LinuxQuestions' and also matches 'LinuxAnswers', 'Linux(Questions|Answers)' matches just the literal string 'Linux(Questions|Answers)'. If you use grep -E for extended mode, the sense of '\(' and '(' is reversed.

+1 to SecretCode's testing suggestions.


All times are GMT -5. The time now is 10:40 PM.