[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bug: Inconsistancy in the use of double backslash
From: |
Aharon Robbins |
Subject: |
Re: bug: Inconsistancy in the use of double backslash |
Date: |
Mon, 25 Apr 2005 12:39:18 +0300 |
Greetings.
This isn't a gawk bug. The first argument to gsub is a regular
expression. You're passing a string constant, which is evaluated
twice, first as a string by the awk parser, and then again by
the regular expression compiler. Thus the regex matcher is
looking for a real newline. If you use four backslashes, it
works:
$ echo '\n' | gawk '{gsub("\\\\n","x",$0);print $0}'
x
This is documented in the gawk manual.
The short answer is to always use regex constants with gsub and you'll
be much less surprised.
Arnold
> Date: Fri, 22 Apr 2005 11:00:07 +0000
> From: Paul Sorensen <address@hidden>
> Subject: bug: Inconsistancy in the use of double backslash
> To: address@hidden
>
> This applies to version 3.1.3 and 3.1.4 of gawk on Linux.
>
> I noticed some inconistancies in the use of backslash to escape the
> backslash. Here is an example:
>
> echo '\n' | gawk '{gsub("\\n","x",$0);print $0}'
>
> One would expect the \n (lteral, not newline) to be replaced by x,
> however; gsub fails to perform the substitution. You still get \n.
>
> Similarly, sub and match fail to match any pattern that includes
> a double backslash.
>
> However, the following line:
> echo '\n' | gawk '/\\n/ {print "found"}'
> Prints found as expected.
>
> Also the line:
> /[\\]n/ {print "found"}
> Works as expected.
>
> However the line:
> echo '\n' | gawk '{gsub("[\\]n","x",$0);print $0}'
> Causes gawk to become confused and tries to escape the ] character.
>
> I have used gawk regularly for many years and this is the first bug
> I've seen. Keep up the good work.
>
> Paul