0

I'm interested in finding pattern like %CHILD_NAME%, %PARENT_NAME%, %ADDRESS% using regex and preferably recursively in the current directory Following is the grep command I am using

grep -r "(.[A-Z]+[_]*[A-Z]+%)" *

When I use the same regex above at http://www.regexr.com, it does match %CHILD_NAME% but my command is not able to find this pattern in any file in current or sub directory.

1
  • If you are using basic regular expression mode (no -E or -P), you need to use \+ to match one or more. Commented Aug 2, 2014 at 16:07

2 Answers 2

2

By default, grep uses basic regular expression and meta-characters like + lose their meaning and need to be escaped. Remove the capturing group ( ), escape the + quantifiers and use an actual % in place of .

grep -r '%[A-Z]\+[_]*[A-Z]\+%' *

Although, you could probably use the following:

grep -r "%[A-Z_]\+%" *
Sign up to request clarification or add additional context in comments.

1 Comment

This works. Other answers worked too but this does the job in less characters and is relatively easy to understand hence accepting as answer.
0

First of all, you regex is too generic: at matches CHILD_NAME% (without % in the front) as well. A better regex is:

"%[A-Z]+(_[A-Z]+)*%"

Next, it is advisable to use the perl interpretation of regexes using the -P flag:

grep -r -P "%[A-Z]+(_[A-Z]+)*%" .

You can also use the -E flag here (extensive mode).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.