Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep: unescaped ^ or $ not supported with -Pz

I wonder why in the new version of grep (Ubuntu 16.04) my bash script stopped working:

...
COMMIT_REGEX='^\[[A-Z]+-[0-9]+\] \s*\S+(?:.|\n|\r)*\s* \(review: ([a-z]+\.[a-z]+|MYSELF)\)$'

if ! grep -Paz "$COMMIT_REGEX" "$1"; then
...

I get "grep: unescaped ^ or $ not supported with -Pz". I've tried to escape ^ and $ symbols, but it doesn't help.

In Ubuntu 15.10 script works perfectly.

like image 717
BugMaster Avatar asked Sep 10 '25 13:09

BugMaster


1 Answers

It seems that the problem is the result of a bug with grep -Pz (credit to Lars Fischer for finding the relevant report).

I would suggest dropping the -P switch and using -E instead:

commit_re='^\[[A-Z]+-[0-9]+\] \s*\S+(.|\n|\r)*\s* \(review: ([a-z]+\.[a-z]+|MYSELF)\)$'

if ! grep -qEaz "$commit_re" "$1"; then

The only changes that I've made are to change -P to -E and add the -q (quiet) switch, since you're only interested in the return code. You don't really need a non-capturing group, so I changed it to a normal one.

I also don't like to see ALL_CAPS variable names as they should really be reserved for use by the shell.

like image 166
Tom Fenech Avatar answered Sep 13 '25 06:09

Tom Fenech