How to enable interval regular expression in mawk?

303 Views Asked by At

I hit an issue when I run the mawk on Ubuntu 1604:

echo "123-456" | mawk '$0~/^[0-9]{3}/ {print $0}'  

The above command output nothing although the regular pattern matched actually.

Then I tried to run the egrep with the same regular pattern:

echo "123-456" | egrep '^[0-9]{3}'  

It works fine!

Then I looked up the doc of the mawk, it seems the root cause is "Interval expressions were not traditionally available in awk.". The field "{3}" in the regular pattern cause the issue. If I use "[0-9][0-9][0-9]" instead of "[0-9]{3}": , it works fine.
https://invisible-island.net/mawk/manpage/mawk.html https://www.math.utah.edu/docs/info/gawk_5.html

I tried the option --posix' and--re-interval' for the mawk, they don't work both.
Is it possible that can enable the "Interval expressions" in the mawk? My OS is "Ubuntu 16.04.4", the mawk is "1.3.3-17ubuntu2".

Thanks.

2

There are 2 best solutions below

0
RARE Kpop Manifesto On

UPDATED : much cleaner solution :

[g/n/m]awk '$-_~"^"(_="[0-9]")(_)_' 

only slightly longer than egrep syntax

 awk '$-_~"^"(_="[0-9]")(_)_'

 egrep '^[0-9]{3}'

a very hideous solution would be

echo "123-456" | {mawk/mawk2} 'BEGIN { FS = "^$" } /^[0-9][0-9][0-9]/' 

another would be even more clunky

echo "123-456" | {mawk/mawk2} 'BEGIN { FS = "^$" 

    } match($0, "^[0-9]+") && (RLENGTH >= 3)' 

It's very un-ideal of course. Stick with gawk if you have access to it for RE intervals.

0
Andre Wildberg On

Trying with the new version this now works

% mawk -W version
mawk 1.3.4 20230203
regex-funcs:        internal
% echo "123-456" | mawk '$0~/^[0-9]{3}/ {print $0}'
123-456