Pad zeros to a number and assign it to a variable
like i get 1 in $i ,i want it to be $i as 01
Pad zeros to a number and assign it to a variable
like i get 1 in $i ,i want it to be $i as 01
cat file1
1
2
3
4
5
6
cat file1 | sed 's/^/0/g'
01
02
03
04
-
--
Thanks
Namish
any ways thanks for reply..........
i want to pad zero to a run time variable
ie
eg :$i is the variable
if $i =1 it should be like 01
if $i=11 it shoild be 11 itself
i=5
i=`printf "%02s\n" "$i"`
echo "$i"
05
If you use ksh, you can use typeset
[/tmp]$ cat test.ksh
#! /bin/ksh
typeset -RZ2 val
val=1
echo $val
val=11
echo $val
[vivarkey@/tmp]$ ./test.ksh
01
11
[/tmp]$
it worked
hey.....
u can also use
var=$1
case $var in
[0-9]) var=0$var
echo $var;;
*) echo $var;;
esac