Hi all,
I have file with numeric values. I need to pad each value with leading zeros such that total lenght of each value is 16.
Example:
cat tmp.txt
502455
50255
5026
5027
5028
Output
0000000000502455
0000000000050255
0000000000005026
0000000000005027
0000000000005028
Any help is appreciated
Thanks,
Jak
# awk '{printf "%015s\n", $0}' file
000000000502455
000000000050255
000000000005026
000000000005027
000000000005028
printf "%.16d\n" $(<tmp.txt)
How do i pad the hexadecimal values with leading zeroes
hex=`echo "obase=16;$data" | bc`
paddedData=`printf "%s" $hex `
echo $paddedData
results in
0
1
9
A
i want
00
01
09
0A
Any help is appreciated
JAK
paddedData=`printf "%02x" 0x$hex `
Try like this:
"/" > x=A
"/" > echo $x | awk '{printf "%02s\n",$0}'
0A
"/" > x=9
"/" > echo $x | awk '{printf "%02s\n",$0}'
09
I am running a simple test on Cygwin and having this problem. Can someone provide me a fix or alternative?
#!/bin/ksh
data=10
printf "%02s" $data
data=A
printf "%02s" $data
The result should be 10 and 0A
But i am getting
printf: "%02s": invalid conversion specification
0A
So it seems like it is not working when data=10
Stuck again
JAK
%02s is not a valid print format specification for a string in ksh
No luck with my suggestion? Seems to work fine for me under bash.
fpmurphy,
Why is it working in bash and not ksh? Is there a alternative i can acheive in ksh instead?
i basically want to pad with leading 0
no hexadecimal here; straight up strings
data=10; output should be 10
data=A; output should be 0A
data=2; output should be 02
i tried in bash and its working. I need to get this going in ksh
Please help
JAK
Again, this issue with ksh is just only when i run on Cygwin - i have installed pdksh package.
jakSun8, no need to PM me, and you still don't appear to have tried my suggestion, which works in bash, ksh (under HP-UX and Linux), ...