LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-01-2012, 04:05 PM   #1
sethbw
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Rep: Reputation: Disabled
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
 
Old 10-01-2012, 04:31 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,057

Rep: Reputation: Disabled
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/*"
 
Old 10-01-2012, 05:56 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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;' *
 
Old 10-02-2012, 09:45 AM   #4
sethbw
LQ Newbie
 
Registered: Apr 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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(' *

Last edited by sethbw; 10-02-2012 at 09:48 AM.
 
Old 10-02-2012, 01:52 PM   #5
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
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?
 
1 members found this post helpful.
Old 10-02-2012, 02:44 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by sethbw View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep regular expressions rahularjun86 Linux - Newbie 5 12-03-2011 04:41 PM
[SOLVED] Regular Expressions and Grep metallica1973 Linux - General 8 03-08-2011 08:17 AM
[SOLVED] Help using wc and grep with regular expressions citygrid Linux - Newbie 12 04-17-2010 02:29 AM
[SOLVED] Why this grep command with regular expressions not working on my system? Andrew Dufresne Linux - Newbie 12 10-01-2009 02:38 PM
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 04:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration