It seems that mawk, and also gawk in POSIX mode, handle backslashes
in the (g)sub replacement strings strangely. For example:
% echo 'hello' | mawk '{ sub(/e/, "\\\\"); print }'
h\llo
% echo 'hello' | gawk '{ sub(/e/, "\\\\"); print }'
h\\llo
% echo 'hello' | POSIXLY_CORRECT=1 gawk '{ sub(/e/, "\\\\"); print }'
h\llo
This causes errors when the script tries to use gsub to restore
the double backslashes on these implementations. There seems to
be no trouble substiting "\\" to get a single backslash, so we
can work around the problem by doubling up the characters being
replaced instead (which additionally requires an adjustment to
the length calculations to reverse this).