Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of grep with regular expressions

Tags:

regex

linux

grep

I am trying to use a regular expression with grep command of Linux

(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))

When I am trying it out at https://www.regextester.com with the contents of a file, I am getting the required result, i.e., the required fields are getting matched but when I am trying to use it as

grep '(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))' file1

all it gives me is a null!

What's the problem here?

like image 579
Kiran Vemuri Avatar asked Jun 13 '12 11:06

Kiran Vemuri


2 Answers

I don't think grep understands character classes like \w and \s. Try using either grep -E or egrep. (grep -E is equivalent to egrep, egrep is just shorter to type.)

So your command would be:

egrep '(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))' file1
like image 174
Tim Pote Avatar answered Sep 29 '22 03:09

Tim Pote


pcregrep -M  '(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))'

did the trick :)

like image 22
Kiran Vemuri Avatar answered Sep 29 '22 02:09

Kiran Vemuri