grep with regular expression in command line
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.
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_]+%" *
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).
上一篇: 包含grep的参数
下一篇: 在命令行中使用正则表达式的grep