In article <msaupi$jas$
1...@dont-email.me>,
Ed Morton <
morto...@gmail.com> wrote:
>On 9/3/2015 7:01 AM, Kenny McCormack wrote:
>> In article <ms9c19$hus$
1...@dont-email.me>,
>> Ed Morton <
morto...@gmail.com> wrote:
>> ...
>>> Thanks for the info. This new warning is like if sed suddenly started issuing
>>> warnings if you wrote `s/foo/bar/` instead of `s/foo/bar/1`. Since others are
>>> seeing it too I'll follow up with
bug...@gnu.org.
>>>
>>> Ed.
>>
>> It's not a bug. The documentation was always clear that the acceptable
>> values were either 1 or G/g. You were always in violation of that. Your
>> sed analogy is completely bogus (as expected).
>>
>> BTW, I lobbied for (and got) this better error reporting into gensub().
>>
>
>Great work Kenny, a valuable contribution to the community. Maybe you now could
>lobby for a warning message when the 2nd arg to substr() is out of range.
Incidentally, just for the benefit of those whose "Google-foo" may not be
quite up-to-snuff, here's the post where I noted that this bug had been
fixed. Note that the script in question, the very useful "comma" function,
was, in fact, written by Ed. So, we have Ed to thank for the finding and
fixing of this little bug. As George Carlin says in one of his routines,
"Thanks, Ed!"
--- Cut Here ---
In article <m51vh4$bcd$
2...@news.xmission.com>,
Kenny McCormack <
gaz...@shell.xmission.com> wrote:
>Some things I noticed about gensub()'s "how" arg:
>
>1) In 3.1.4, it seems like the "number other than 1" functionality is not
>implemented. I.e., it looks like if the value is anything other than a
>string matching /[Gg].*/, it is taken as being "1". In 3.1.8, it works
>correctly, so I assume it got fixed betwen those two versions. Can anyone
>confirm this for me? Note that although I have binaries (executables) for
>these old versions, I don't have man pages for them, so I can't check for
>myself. Actually, what I'm really asking is "Does the man page for 3.1.4
>document it as doing what it should be doing or does it document what it
>actually was doing (in that version) ?"
>
>2) In the current version, if you pass "0" for "how", you get a warning,
>but passing a negative value (or any other "garbage" string) generates no
>warning (but all such values are treated as if they were 1):
>
>$ enhance gawk4 '{ print gensub($1,$2,$3,$4) }'
>abc DEF 1 abcabcdefghi
>DEFabcdefghi
>abc DEF 2 abcabcdefghi
>abcDEFdefghi
>abc DEF 0 abcabcdefghi
>gawk4: cmd. line:1: (FILENAME=- FNR=3) warning: gensub: third argument of 0 treated as 1
>DEFabcdefghi
>abc DEF -1 abcabcdefghi
>DEFabcdefghi
>abc DEF garbage abcabcdefghi
>DEFDEFdefghi
>abc DEF rubbish abcabcdefghi
>DEFabcdefghi
>$
An update:
This seems to be fixed in 4.1.3. It complains about anything unexpected
(i.e., other than 1 or "G" or "g") in the third arg. See below:
('gawk4' is version 4.1.3, 'gawk41' is version 4.1.1)
% gawk4 'BEGIN { print gensub("abc","DEF",-1,"abcdefghikl")}'
gawk4: cmd. line:1: warning: gensub: third argument -1 treated as 1
DEFdefghikl
% gawk41 'BEGIN { print gensub("abc","DEF",-1,"abcdefghikl")}'
DEFdefghikl
%
Yey! It seems complaints here do get noticed (sometimes...)
Also note, I have the following script installed on various of my systems
to "comma-ize" numbers. I copied it from this board some time back:
--- Cut Here ---
function comma(num) {
if (num < 0)
return "-" comma(-num)
while (num != (num=gensub(/([0-9])([0-9][0-9][0-9])($|[,.])/,"\\1,\\2\\3","",num)));
return num
}
--- Cut Here ---
Note the use of "" as the 3rd arg to gensub(). I'm sure that was in the
original (i.e., whoever originally wrote and posted the script had that).
The funny thing is that this script worked just fine right up until I
compiled and started using 4.1.3, which as we've seen now generates a
warning for that usage. So, I had to change it to use 1 instead of "".
--
The scent of awk programmers is a lot more attractive to women than
the scent of perl programmers.
(Mike Brennan, quoted in the "GAWK" manual)
--- Cut Here ---