[comp.bugs.sys5] nawk sub/gsub bug

eric@mks.com (Eric Gisin) (05/11/91)

$ uname -r
3.2
$ echo .xx. | sed 's/xx*/\\&\\/'
.\xx\.
# note that "\\" passes a single \ to sub.
$ echo .xx. | nawk '{sub(/x+/,"\\&\\"; print}'
.&\.
$ echo .xx. | nawk '{sub(/x+/,"\\\\&\\\\"; print}'
.\&\\.

I don't know whether its a feature or a bug, but you can't use sub
to enclose a substring with \'s, as the sed example does.
The sub/gsub algorithm should be modified to replace \& with &,
\\ with \, and leave all other escapes alone.

arnold@audiofax.com (Arnold Robbins) (05/14/91)

In article <1991May10.175555.10306@mks.com> eric@mks.com (Eric Gisin) writes:
|$ uname -r
|3.2
|$ echo .xx. | sed 's/xx*/\\&\\/'
|.\xx\.
|# note that "\\" passes a single \ to sub.
|$ echo .xx. | nawk '{sub(/x+/,"\\&\\"; print}'
|.&\.
|$ echo .xx. | nawk '{sub(/x+/,"\\\\&\\\\"; print}'
|.\&\\.
|
|I don't know whether its a feature or a bug, but you can't use sub
|to enclose a substring with \'s, as the sed example does.
|The sub/gsub algorithm should be modified to replace \& with &,
|\\ with \, and leave all other escapes alone.

The bug, if such, is in the original design of awk.  sub and gsub weren't
intended to be as general purpose as the 's' command in ed(1).  Your
dismay is understandable, but I think this one of those things that are
too late to fix.

Because of this, we're considering an edsub(string, pat, rep, globalflag)
function for a future version of gawk....
-- 
Arnold Robbins				 AudioFAX, Inc. | Threads are the
2000 Powers Ferry Road, Suite 200 / Marietta, GA. 30067 | lack of an idea.
INTERNET: arnold@audiofax.com  Phone:   +1 404 618 4281 |     -- Rob Pike
UUCP:	  emory!audfax!arnold  Fax-box: +1 404 618 4581 |