In the case of nawk
, the answer is: not without modifying the source.
Two of the problems are:
- regular expressions are part of the language (
~
and//
), as well as the defined language functions (match()
etc.) nawk
uses its own regex code (in the fileb.c
) so unlike a program which uses one regex library, using a different library with alternate implementations ofregcomp()
regexec()
will not help.
One way gawk
has approached this is to extend match()
with a third argument. (There's also gensub()
as you note, but I try to avoid it where possible.)
gawk
also supports loadable extensions, which would be a way to interface with a PCRE library to provide new "builtin" functions (though not replace ~
or any internal functions). This API is the new "4.1" way of doing extensions, previous versions had a substantially different implementation.
Finally, one nawk
way to achieve the required substitution is:
match($0,/\|[^|]*\|/) {
do {
sub(/\|[^|]*\|/,"\\abs{" substr($0,RSTART+1,RLENGTH-2) "}",$0)
} while (match($0,/\|[^|]*\|/))
}
{ print }