Operating System - HP-UX
1838665 Members
5704 Online
110128 Solutions
New Discussion

Re: Fill numbers with 0 in shell

 
SOLVED
Go to solution

Fill numbers with 0 in shell

Hi everybody,
Can someone of you help me with this?
I have numbers from 1 to 9999 and i want to fill the number with 0s until getting 4 characters. I mean:
1 ---- 0001
2 ---- 0002
.
.
25 --- 0025
.
.
9999 --- 9999

Would you have a way or command in shell to do it?. My system is running on HP-UX B11.11.

Regards,
Christian Aguilar
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Fill numbers with 0 in shell

Hi:

Use formatted printing:

#!/usr/bin/sh
for n in 1 2 3
do
printf "%04d\n" ${n}
done


Regards!

...JRF...
Hasan  Atasoy
Honored Contributor

Re: Fill numbers with 0 in shell


let i=0

while [ $i -lt 9999 ]
do
printf "%04d\n",$i
let i=$i+1
done




Hasan

Re: Fill numbers with 0 in shell

The advices were good for my work.
Christian Aguilar
Dennis Handly
Acclaimed Contributor

Re: Fill numbers with 0 in shell

You can also use the shell directly to do this:
$ typeset -Z4 i=9; echo $i
0009