Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The awk bug

0 views
Skip to first unread message

Kaplenko Vitaliy

unread,
Nov 27, 2009, 11:31:23 AM11/27/09
to bug-...@gnu.org
If run
in Linux localhost.localdomain 2.6.29.5-191.fc11.i686.PAE #1 SMP Tue Jun
16 23:19:53 EDT 2009 i686 i686 i386 GNU/Linux:

$ echo "./some_script" | awk 'gsub(".","a") {print $0}'
aaaaaaaaaaaaa

But must be:

$ echo "./some_script" | awk 'gsub(".","a") {print $0}'
a/some_script


John Cowan

unread,
Nov 27, 2009, 3:21:32 PM11/27/09
to Kaplenko Vitaliy, bug-...@gnu.org
Kaplenko Vitaliy scripsit:

> $ echo "./some_script" | awk 'gsub(".","a") {print $0}'
> aaaaaaaaaaaaa
>
> But must be:
>
> $ echo "./some_script" | awk 'gsub(".","a") {print $0}'
> a/some_script

Not so. The first argument of gsub is interpreted as a regular
expression, and "." in a regular expression means "any character".
Consequently, all characters in the input are replaced. You must use
"\." to match a literal dot.

> $ echo "some_script" | awk 'gsub("/","a") {print $0}'
> $
>
> But must be:
>
> $ echo "some_script" | awk 'gsub("/","a") {print $0}'
> $ some_script

Also not the case. You have placed the call to gsub into the pattern
part of the pattern-action statement. Consequently, it evaluates to 0
because there are no slashes in the input, and therefore the action is
never executed. A correct action statement would be

{gsub("/", a); print $0}

--
A rabbi whose congregation doesn't want John Cowan
to drive him out of town isn't a rabbi, http://www.ccil.org/~cowan
and a rabbi who lets them do it co...@ccil.org
isn't a man. --Jewish saying


0 new messages