0

The man pages for grep on Ubuntu 18.04 and CentOS 8 have this to say about the -P option for grep:

-P, --perl-regexp Interpret the pattern as a Perl-compatible regular expression (PCRE). This is experimental and grep -P may warn of unimplemented features.

I have put emphasis on "experimental" and "may warn". Experimental implies it's not suitable for production. "may warn" implies that you could run into an unimplemented feature and grep could fail to warn you about it.

On older versions of grep, I have even seen the phrase "highly experimental" instead of just "experimental".

Is the -P option to GNU grep safe to use in production? Some of my bootstrapping scripts need certain perl features, so this option is quite useful to me when perl has not already been installed/included.

3
  • 4
    grep -P is of course absolutely non-portable; but if it works on your system, why not just use it? grep -P is based on the pcre library, which is already used all over the place. IMLE it's grep -E which may get pathologically slow on some innocent regexes + inputs which grep -P will breeze through (but, unfortunately I don't have any examples at hand). – mosvy Jan 20 '20 at 22:02
  • @mosvy Yes, I am aware that it is not portable. But it is available out of the box on CentOS 8 minimal which does not ship with perl. On FreeBSD I can use fold hacks to work with grapheme clusters (I was actually shocked that it's implementation of fold even supported grapheme clusters). I just like to know I have options available out of the box before I have started installing packages. – Harold Fischer Jan 21 '20 at 18:09
  • 1
    @mosvy sample you needed? askubuntu.com/q/960557/283843 – αғsнιη Jan 21 '20 at 18:13
1

This question is probably a matter of taste.

Personally I would try to see if I could rewrite the regexp, so it only uses POSIX. If that is not possible I would use:

| perl -ne '/.../ and print' |

instead. This way I am sure it is supported, and it is clear what the error is if perl is not installed.

I do use -P for personal scripts (i.e. non-production).

The only time I think it may be justified to be used in production is if your scripts make a test that -P is supported and raise a clear error if it is not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.