Format a number with zeros in Unix shell

Solved
tbeghain Posted messages 63 Status Membre -  
 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
Configuration: AIX 5.2

8 réponses

jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
Hello,
[tmpfs]$ for i in $(seq 10); do printf "%02d\n" "$i"; done 01 02 03 04 05 06 07 08 09 10 
;-))

--
Z'@+...che.
JP : Zen, my Nuggets ! ;-) Knowledge is only good when it is shared.
2
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
That's no longer synchronization
It's at a higher level, telepathy for example ;-))
--
lami20j
0
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898 > lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention  
 
It's clear ;-))

But there must have been some interference, I didn't quite catch the "20" ;-DDDDD

--
Z'@+...che.
JP : Zen, my Nuggets ! ;-) Knowledge is only valuable if it's shared.
0
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
Hello,

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
1
Gotrune Posted messages 55 Registration date   Status Membre 3
 

'0'&$1
0
tbeghain Posted messages 63 Status Membre 3
 
Thank you very much, the printf does exactly what I wanted.
0
tbeghain Posted messages 63 Status Membre 3
 
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?
0
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
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.
JP : Zen, my Nuggets ! ;-) Knowledge is only good when it is shared.
0
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
Hello,

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
0
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
You math nerd ;-))

Thank you.

--
Z'@+...che.
JP : Zen, my Nuggets ! ;-) Knowledge is only good if it is shared.
0
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570
 
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
 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
0
jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention   4 898
 
Thank you very much lami20j ;-))

--
Z'@+...che.
JP : Zen, my Nuggets ! ;-) Knowledge is only good if it is shared.
0
lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention   3 570 > jipicy Posted messages 40842 Registration date   Status Modérateur Last intervention  
 
You're welcome :-))
--
lami20j
0
eko > lami20j Posted messages 21506 Registration date   Status Modérateur, Contributeur sécurité Last intervention  
 
I would need help; I have the same problem, except that I would like to do it with numbers in base 16.
Thank you in advance.
0