I've hit a case in which /ere/ doesn't expand the same as
"$0 ~ /ere/" which it should do according to the POSIX spec.
The goal was to meet the criterion "one and only one of multiple
regex matches", so I used
jot 20 | awk '/1/ + /5/ == 1'
(this can be expanded for any number of expressions, e.g.
"/1/ + /5/ + /7/ == 1", but the example using `jot 20` makes it
easier to demonstrate the problem, looking for lines containing 1 or 5
but not 15)
This gives a parse error:
$ jot 20 | awk '/1/ + /5/ == 1'
awk: syntax error at source line 1
/1/ + >>> / <<<
awk: bailing out at source line 1
Strangely, wrapping the expressions in parens works as expected:
$ jot 20 | awk '(/1/) + (/5/) == 1'
However manually performing the replacement documented above
according to the POSIX spec:
$ jot 20 | awk '$0 ~ /1/ + $0 ~ /5/ == 1'
parses fine (instead of giving the syntax error), so awk isn't doing the
"/ere/ -> $0 ~ /ere/" replacement POSIXly. However, this also doesn't
give results I'd consider correct (it returns "5" and "15"). Again,
wrapping those expansions in parens gives the expected/correct results:
$ jot 20 | awk '($0 ~ /1/) + ($0 ~ /5/) == 1'
As a side note, gawk parses the original notation ('/1/ + /5/ == 1')
fine and it does the same as the parenthesized versions above.
When an ERE token appears as an expression in any context other than
as the right-hand of the '˜' or "!˜" operator or as one of the
built-in function arguments described below, the value of the
resulting expression shall be the equivalent of:
$0 ˜ /ere/
I'll look into this. It seems related to other bugs in awk we have already
(In reply to Warner Losh from comment #1)
still a bug after the last import
Also, upstream has indicated that this may not be fixed.
is the issue I've filed upstream.
I've reported this bug twice to upstream now https://github.com/onetrueawk/awk/issues/49 and https://github.com/onetrueawk/awk/issues/122. Both times they have rejected it. I'm closing this bug as it's not FreeBSD specific. If you are interested in pursing it, I can only recommend you find a fix for this and work with upstream to get it accepted. I'm sorry I don't have a better outcome here, though.