nlta
nlta

Reputation: 1914

Posix awk escaping in sub()

What is the posix awk compliant result of this awk program (if any?), and why do mawk and goawk not agree with gawk and onetrueawk?

BEGIN { a = "a"; sub("a", "-\\\\a-", a); print a; }
$ mawk -f prog.awk                                         Mon Jan  9 20:12:52 2023
-\a-
$ goawk -f prog.awk                                        Mon Jan  9 20:13:02 2023
-\a-
$ gawk -f prog.awk                                         Mon Jan  9 20:12:58 2023
-\\a-
$ onetrueawk -f prog.awk                                   Mon Jan  9 20:12:59 2023
-\\a-

Note that the above program is copied from a file and not run through any escaping my shell might do.

Upvotes: 4

Views: 96

Answers (1)

nlta
nlta

Reputation: 1914

The behavior exhibited by gawk is non-posix. If you run gawk with gawk --posix -f prog.awk you see the posix behavior expected: the string is first unescaped lexically and then unescaped again when parsed as an ERE arg to sub.

$ gawk --posix -f prog.awk
-\a- 

Gawk briefly tried to standardize on the posix behavior in 4.0.0 but this broke backwards compatibility and was quickly reverted. Source: the gawk manual

Upvotes: 1

Related Questions