How to specify regex quantifiers with mawk

mawkregular expression

I'm familiar with the concept of specified bounded regex quantifiers as follows:

Quantifier  Legend               Example    Sample Match

{3}         Exactly three times  \D{3}      ABC
{2,4}       Two to four times    \d{2,4}    156
{3,}        Three or more times  \w{3,}     regex_tutorialer

However, I've not been able to find anything like the above for mawk in the man pages nor online.

Does the mawk command have this functionality, or is there a different way of accomplishing the same effect?

I am using version 1.3.3

Best Answer

Interval regexp operators are supported in POSIX compliant awk implementations.

But as awk initially didn't support them (neither did nawk nor mawk nor gawk), there are still several implementations that don't support them like mawk, the one true awk (originally maintained by Brian Kernighan, the k in awk) until a few days ago, Solaris /bin/awk, Solaris /bin/nawk, the awk of most BSDs.

Like for egrep, several implementations objected to adding support for them as they would break backward compatibility (there was no similar problem for \{x,y\} in BREs as used by grep).

\w, \d, \D are perl regexp extensions which are generally not supported (busybox awk and gawk (when not in POSIX mode) support \w). The standard equivalents would be [[:alpha:]_], [[:digit:]], [^[:digit:]] respectively, but are not supported by mawk yet¹.

On Solaris, you'll want to use /usr/xpg4/bin/awk.

With older versions of GNU awk, you had to use the --re-interval option, or start it with POSIXLY_CORRECT=anything in the environment for the regex intervals to be supported.

With implementation that don't support them, you can use combinations of ?, + and *:

  • x{1,3} -> xx?x? or (x|xx|xxx)
  • x{1,} -> x+
  • x{0,} -> x*
  • x{3,} -> xxx+ or xxxx*
  • x{3,6} -> xxxx?x?x?
  • etc.

¹ anyway, mawk doesn't support localisation or multi-byte characters, so you might as well restrict to ASCII characters and use [_a-zA-Z], [0-9] and [^0-9]

Related Question