Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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...

    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

  2. #2
    Linux User
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    312
    Quote Originally Posted by sethbw View Post
    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...

    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
    The thing to remember about "regular expressions" is that they aren't regular. Meaning, there are differences in the way they are implemented in awk, bash, sed, grep, perl, etc... and you have to use the variant that matches the tool in question. For "grep" and the patterns you're looking for, just get rid of all the "\" characters and it will work.

  3. #3
    Hmmm isn't this example:

    grep -r 'radius(' *

    the exact opposite of what the man page says below?
    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. It sounds like its saying you HAVE to use the backslashes... GRRRR.

    Why then is it that when I try with the backslash in front of the parenthesis this is the return:

    $ 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:

    $ grep -r 'radius(' *


    I will have to agree - it appears to not want the backslashes for escaping special characters, however it does not explain the "border-radius(" phenomena.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux User
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    312
    First of all, it's great that you read the man pages. They can be worded like a contract poorly translated from a foreign language sometimes, but there's lot of good info so it's worth the frustration.

    Having said that, my interpretation of the section you quoted it that the listed special characters are treated "as is" without any special meaning normally. And you preface them with "\" if you want "grep" to treat them like regular expression operators. So it's the opposite of what you assumed and "(" means "just look for a left paren".

    As for why '-radius(' doesn't work, that's about how grep parses arguments to the command not how it interprets regular expressions. The leading "-" causes the rest of the string to be interpreted as flags not a text search pattern. So you'll need to do this to search for that string,

    Code:
    grep -r '\-radius(' *
    to get the behavior you want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •