Format a number with zeros in Unix shell
Solved
tbeghain
Posted messages
63
Status
Membre
-
eko -
eko -
Hello,
I would like to format a number in Unix shell so that it always has two numeric positions. For example, 1 becomes 01, 2 becomes 02, etc...
Of course, I can do this with a bit of programming, but is it possible to do it with a simple command?
Thank you
I would like to format a number in Unix shell so that it always has two numeric positions. For example, 1 becomes 01, 2 becomes 02, etc...
Of course, I can do this with a bit of programming, but is it possible to do it with a simple command?
Thank you
Configuration: AIX 5.2
8 réponses
Hello,
there is the printf command
--
lami20j
there is the printf command
root@debian:~# for i in $(seq 20);do printf "%02d\n" $i;done 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
--
lami20j
Sorry, but I have a weird little thing with printf specifically with the numbers 8 and 9 exclusively
When running the following script:
AA=` printf "%02d\n" "07" `
echo $AA
AA=` printf "%02d\n" "08" `
echo $AA
AA=` printf "%02d\n" "09" `
echo $AA
AA=` printf "%02d\n" "10" `
echo $AA
AA=` printf "%02d\n" "7" `
echo $AA
AA=` printf "%02d\n" "8" `
echo $AA
AA=` printf "%02d\n" "9" `
echo $AA
I get the following results
07
printf: 08 not completely converted
00
printf: 09 not completely converted
00
10
07
08
09
Why do 08 and 09 not work???? while all other numbers pass???? Is it due to my Unix? Who has cursed the 8 and 9?
When running the following script:
AA=` printf "%02d\n" "07" `
echo $AA
AA=` printf "%02d\n" "08" `
echo $AA
AA=` printf "%02d\n" "09" `
echo $AA
AA=` printf "%02d\n" "10" `
echo $AA
AA=` printf "%02d\n" "7" `
echo $AA
AA=` printf "%02d\n" "8" `
echo $AA
AA=` printf "%02d\n" "9" `
echo $AA
I get the following results
07
printf: 08 not completely converted
00
printf: 09 not completely converted
00
10
07
08
09
Why do 08 and 09 not work???? while all other numbers pass???? Is it due to my Unix? Who has cursed the 8 and 9?
I assure you that under GNU/Linux I have exactly the same problem and I don't know why!?
I have tried all forms of "quotation" in all kinds (i>single, double, with, without, etc.) and nothing works!!!
Even without using a variable, in pure command line, it crashes ;-((
Any solutions, leads or hints of explanation will be welcome.
Thank you.
--
Z'@+...che.
I have tried all forms of "quotation" in all kinds (i>single, double, with, without, etc.) and nothing works!!!
Even without using a variable, in pure command line, it crashes ;-((
Any solutions, leads or hints of explanation will be welcome.
Thank you.
--
Z'@+...che.
JP : Zen, my Nuggets ! ;-) Knowledge is only good when it is shared.
Hello,
try this
--
lami20j
try this
lami20j@debian:~$ cat ccm.sh #!/bin/bash AA=` printf "%02.f\n" "07" ` echo $AA AA=` printf "%02.f\n" "08" ` echo $AA AA=` printf "%02.f\n" "09" ` echo $AA AA=` printf "%02.f\n" "10" ` echo $AA AA=` printf "%02.f\n" "7" ` echo $AA AA=` printf "%02.f\n" "8" ` echo $AA AA=` printf "%02.f\n" "9" ` echo $AA lami20j@debian:~$ sh ccm.sh 07 08 09 10 07 08 09
--
lami20j
When you write 01, 02 ... 08, 09 the numbers are interpreted as octal
in octal 8 = 10 and 9 = 11
An octal number cannot contain digits beyond 7 inclusive
however when you write 8 and 9 these are decimal numbers so I can convert them to octal
--
lami20j
in octal 8 = 10 and 9 = 11
An octal number cannot contain digits beyond 7 inclusive
lami20j@debian:~$ printf "%o and %o and %o\n" 0777 777 521 777 and 1411 and 1011 lami20j@debian:~$
however when you write 8 and 9 these are decimal numbers so I can convert them to octal
lami20j@debian:~$ printf "%o and %o\n" 8 9 10 and 11
--
lami20j
It's at a higher level, telepathy for example ;-))
--
lami20j
But there must have been some interference, I didn't quite catch the "20" ;-DDDDD
--
Z'@+...che.