Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Some observations re: gensub() (and a question about versions)

213 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Kenny McCormack

ungelesen,
25.11.2014, 08:17:2525.11.14
an
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
$

--
Modern Conservative: Someone who can take time out from flashing her
wedding ring around and bragging about her honeymoon to complain that a
fellow secretary who keeps a picture of her girlfriend on her desk is
"flauting her sexuality" and "forcing her lifestyle down our throats".

rod...@yossman.net

ungelesen,
13.12.2014, 20:46:5813.12.14
an
On Tuesday, November 25, 2014 5:17:25 AM UTC-8, Kenny McCormack wrote:
> Some things I noticed about gensub()'s "how" arg:

see man pages/sources for gawk - try:
ftp://ftp.gnu.org/gnu/gawk/
or
http://ftp.gnu.org/gnu/gawk/

suspect you need:
ftp://ftp.gnu.org/gnu/gawk/gawk-3.1.4-doc.tar.gz
and
ftp://ftp.gnu.org/gnu/gawk/gawk-3.1.8.tar.gz

:rod
--
"Nothing to see here, move along folks"

Kenny McCormack

ungelesen,
29.05.2015, 12:58:3929.05.15
an
In article <m51vh4$bcd$2...@news.xmission.com>,
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)
0 neue Nachrichten