Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Feb 2019 16:51:16 +0000
From:      bugzilla-noreply@freebsd.org
To:        standards@FreeBSD.org
Subject:   [Bug 235887] awk fails to replace "/ere/" with "$0 ~ /ere/" according to POSIX
Message-ID:  <bug-235887-99@https.bugs.freebsd.org/bugzilla/>

next in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D235887

            Bug ID: 235887
           Summary: awk fails to replace "/ere/" with "$0 ~ /ere/"
                    according to POSIX
           Product: Base System
           Version: CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Many People
          Priority: ---
         Component: standards
          Assignee: standards@FreeBSD.org
          Reporter: freebsd@tim.thechases.com

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[0].

The goal was to meet the criterion "one and only one of multiple
regex matches", so I used

  jot 20 | awk '/1/ + /5/ =3D=3D 1'

(this can be expanded for any number of expressions, e.g.=20
"/1/ + /5/ + /7/ =3D=3D 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/ =3D=3D 1'
  awk: syntax error at source line 1
   context is
          /1/ + >>>  / <<<=20
  awk: bailing out at source line 1

Strangely, wrapping the expressions in parens works as expected:

  $ jot 20 | awk '(/1/) + (/5/) =3D=3D 1'

However manually performing the replacement documented above
according to the POSIX spec:

  $ jot 20 | awk '$0 ~ /1/ + $0 ~ /5/ =3D=3D 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/) =3D=3D 1'

As a side note, gawk parses the original notation ('/1/ + /5/ =3D=3D 1')
fine and it does the same as the parenthesized versions above.

-tkc

[0] """

When an ERE token appears as an expression in any context other than
as the right-hand of the '=CB=9C' or "!=CB=9C" operator or as one of the
built-in function arguments described below, the value of the
resulting expression shall be the equivalent of:

$0 =CB=9C /ere/

"""
http://pubs.opengroup.org/onlinepubs/009695399/utilities/awk.html

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-235887-99>