1753859 Members
7629 Online
108809 Solutions
New Discussion юеВ

printf shell command

 
SOLVED
Go to solution
Wouter Jagers
Honored Contributor

printf shell command

Hi all,

I just bounced into something strange. Can anyone explain the below ?

------------------------------
..
$ printf "%02d\n" 05
05
$ printf "%02d\n" 06
06
$ printf "%02d\n" 07
07
$ printf "%02d\n" 08
printf: Error converting 08
00
$ printf "%02d\n" 09
printf: Error converting 09
00
$ printf "%02d\n" 10
10
$ printf "%02d\n" 8
08
$ printf "%02d\n" 9
09
------------------------------

As you can see, for some reason printf doesn't like 08 or 09, but acts as expected for the others.. 8 and 9 (without the leading zero) also seems ok.

Is this fishy, or is it monday again and am I missing something blatantly obvious ?

Cheers
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
9 REPLIES 9
Peter Godron
Honored Contributor
Solution

Re: printf shell command

Wout,
By prefixing with zero the value is read as octal.
Try printf "%02d\n" 012 and you get 10, which is 8+2
Wouter Jagers
Honored Contributor

Re: printf shell command

Isn't that %o (o, not zero) ?

---
$ printf "%02d\n" 10
10
$ printf "%o\n" 10
12
---

The plan is to convert any one or two digit integer to a two digit one, leading zero if applicable.

I could use sed just as well, but I'm intrigued about my 'issue' ;-)
an engineer's aim in a discussion is not to persuade, but to clarify.
Wouter Jagers
Honored Contributor

Re: printf shell command

Ohhhh, I see. Sorry about that last one.
It is monday for sure :-p

Thanks !
an engineer's aim in a discussion is not to persuade, but to clarify.
Peter Godron
Honored Contributor

Re: printf shell command

Wout,
your code is right, just don't pass the leading zero as input!

printf "%02d\n" 1 => 01
printf "%02d\n" 9 => 09
printf "%02d\n" 10 => 10
etc.
Peter Nikitka
Honored Contributor

Re: printf shell command

Hi,

you mix input- and output specifications:
The %-specifier is an output directive - you specify this.
For the input, the printf knows nothing about a typedef or so - it just sees a string. This string - the 08 - is scanned heuristically for a valid input specification. The convention is now, e.g.
0N - octal number N
0xNN - hex number NN

Any "violation" will be reported; the diagnostic output will differ between implementations of printf (e.g. printf: 08 not completely converted.).

To get your requested output format, use:
typeset -Z2 j
typeset -i i=0
while [ i -lt 12 ]
do
j=$i
print $j
((i+=1))
done

The output:
00
01
02
03
04
05
06
07
08
09
10
11

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Wouter Jagers
Honored Contributor

Re: printf shell command

Yes, only the thing is my little procedure is accepting day/month fields which can be one or two digits, so "8" and "08" can both come in, depending on input.

I've quickly sed'ded the leading zero away, no problem there.. Thanks for sorting out my confusion, I was really dazzled there for a second :-)

Cheers
an engineer's aim in a discussion is not to persuade, but to clarify.
Peter Nikitka
Honored Contributor

Re: printf shell command

Hi,

the typeset -Z2 will work for this as well:

typeset -Z2 j=08
print $j
08

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Wouter Jagers
Honored Contributor

Re: printf shell command

All confusion was cleared. Thanks guys !
an engineer's aim in a discussion is not to persuade, but to clarify.
Bill Hassell
Honored Contributor

Re: printf shell command

And for completeness, old version of the POSIX shell treated leading zeros as decimal so after patching (about 3-4 years ago), some scripts would break (as they should). Octal interpretation was inconsistent in the sh-posix shell. See PHCO_25597 and PHCO_26789.


Bill Hassell, sysadmin