0

Need to improve a regex for awk. It works on regex101.

/vault-(?!etcd|operator)/

Should only match:

vault-etcd-abc123
vault-operator-def456
vault-ghi789 <--

Tried:

awk '/vault-(?!etcd|operator)/ {print $1;exit}'

awk: illegal primary in regular expression vault-(?!etcd|operator) at !etcd|operator)

I have tried escaping some of the special characters such as |, but then it does not give the expected results.

3
  • 4
    Awk understands extended regular expressions, not PCREs. That site is a poor site to use for testing regular expressions as it doesn't even support POSIX basic or extended expressions, as far as I can see.
    – Kusalananda
    Apr 26 '19 at 11:15
  • Related: unix.stackexchange.com/questions/119905/…
    – Kusalananda
    Apr 26 '19 at 11:19
  • rexv should be a better site. Apr 27 '19 at 8:01
2

(?!...) is a perl regular expression operator while awk regexps are a variant of the POSIX extended regular expressions (ERE).

(?!...) is not incompatible with EREs as (? is unspecified in EREs, so awk implementations could choose to implement that operator as it wouldn't affect portable applications which cannot use it.

But while there are some egrep/grep -E implementations like ast-open's that do support it, I don't know of any awk implementations that does (based on the error message, yours seems to be based on Brian Kernighan's original implementation which may not even support the {x,y} interval operators). awk cannot use PCRE (a portable perl-compatible regexp libraries) as perl regexps as a whole are incompatible with awk REs (like for the alternation operator behaviour, or \b which means something different in perl and awk).

Here, you could use perl instead:

perl -lae 'print $F[0] if /vault-(?!etcd|operator)/'

Or you could use a grep with -o and PCRE support, like pcregrep or GNU grep with it's -P option:

grep -Po '^\s*+\K(?=.*vault-(?!etcd|operator))\S+'

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.