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

printf quirk

65 views
Skip to first unread message

Bruce Horrocks

unread,
Dec 29, 2014, 8:07:30 PM12/29/14
to
The statement:

printf "%3$*d %2$d \n", 4, 5, 6

results in

.....6 5

(where leading spaces have been replaced with dot.)

That is, the value of the parameter is used as both the width format and
the value to print, which is probably not very useful.

There is a related error "fatal: must use `count$' on all formats or
none" which could be issued instead. Is this worth raising a bug report
over? Would people prefer some other behaviour rather than an error?

This is Gawk 4.1.60.
--
Bruce Horrocks
Surrey
England
(bruce at scorecrow dot com)

Ed Morton

unread,
Dec 29, 2014, 11:58:00 PM12/29/14
to
On 12/29/2014 7:07 PM, Bruce Horrocks wrote:
> The statement:
>
> printf "%3$*d %2$d \n", 4, 5, 6
>
> results in
>
> .....6 5
>
> (where leading spaces have been replaced with dot.)
>
> That is, the value of the parameter is used as both the width format and the
> value to print, which is probably not very useful.
>
> There is a related error "fatal: must use `count$' on all formats or none" which
> could be issued instead. Is this worth raising a bug report over? Would people
> prefer some other behaviour rather than an error?
>
> This is Gawk 4.1.60.

As the man page says:

NOTE: There are some pathological cases that gawk may fail to diagnose. In such
cases, the output may not be what you expect. It’s still a bad idea to try
mixing them, even if gawk doesn’t detect it.

Just don't do it or always do it.

Ed.

Michal Jaegermann

unread,
Dec 30, 2014, 9:55:04 PM12/30/14
to
On Tue, 30 Dec 2014 01:07:27 +0000, Bruce Horrocks wrote:

> The statement:
>
> printf "%3$*d %2$d \n", 4, 5, 6
>
> results in
>
> .....6 5
>
> (where leading spaces have been replaced with dot.)

I guess that the following

gawk 'BEGIN{for (i=1; i<=10; i++){printf "%03$*d %2$d \n", 4, 5, i }}'

gives a clearer picture of your trouble. Only that according to my
reading of "Printf Ordering" section of gawk documentation you should be
using instead

gawk 'BEGIN{for (i=1; i<=10; i++){printf "%03$*1$d %2$d \n", 4, 5, i}}'

and that is likely more to your expectations.

It seems that the real issue is that the first form is not flagged as a
syntax error.

Michal

Bruce Horrocks

unread,
Jan 2, 2015, 6:55:31 AM1/2/15
to
On 31/12/2014 02:55, Michal Jaegermann wrote:
> On Tue, 30 Dec 2014 01:07:27 +0000, Bruce Horrocks wrote:
>
>> The statement:
>>
>> printf "%3$*d %2$d \n", 4, 5, 6
>>
>> results in
>>
>> .....6 5
>>
>> (where leading spaces have been replaced with dot.)
>
> I guess that the following
>
> gawk 'BEGIN{for (i=1; i<=10; i++){printf "%03$*d %2$d \n", 4, 5, i }}'
>
> gives a clearer picture of your trouble. Only that according to my
> reading of "Printf Ordering" section of gawk documentation you should be
^^^^^^^^^^^^^^^^^^^^^^^^

Thank-you for pointing this out. I was using Aaron's book rather than
the gnu manual and hadn't realised that * combined with a position param
was supported. D'oh.

> using instead
>
> gawk 'BEGIN{for (i=1; i<=10; i++){printf "%03$*1$d %2$d \n", 4, 5, i}}'
>
> and that is likely more to your expectations.
>
> It seems that the real issue is that the first form is not flagged as a
> syntax error.

I think so.

Regards,
0 new messages