Problem with printf in UNIX KSH shell

[LEFT]Hi ALL,

I am using

SunOS 5.9 and KSH(bin/ksh)

The problem am facing is

error message diaplyed on screen

printf: 12099415.79 not completely converted
printf: + expected numeric value
printf: 11898578.29 not completely converted

When i try printing with

The output is 120994150 18985780 which is not expected !

my requirement of O/P is 000012099415.79+000011898578.29

var1 and var 2 with left padded with zeros to make it 15(including decimal and 2 places after that)

Yes i am confused with printf, so please let me know what exact formatting i should use to get the desired output...[/LEFT]

Try:

printf "%015.2f%c%015.2f\n" $v1 $v3 $v2
1 Like

different shell and OS, but...you're just mixing metaphors with the printf formatting directions. %d is decimal, where you inputs are float...%f. The +-char needs to be treated as a string, and therefore you'd use %s:

~ # printf "%-9d %-1d %-9d\n" 12.79 '+' 11.79                                   
sh: invalid number '12.79'                                                      
0         sh: invalid number '+'                                                
0 sh: invalid number '11.79'                                                    
0                                                                               
~ # printf "%-9f %-1s %-9f\n" 12.79 '+' 11.79                                   
12.790000 + 11.790000                                                           

When you'd started talking about zero-padding though...you can play with the following and see what you'd need:

~ # printf "%017.2f %-1s %017.2f\n" 12.79 + 11.79                               
00000000000012.79 + 00000000000011.79                          
1 Like

Thank you all...
One more question..
will it be possible to use the same printf formatting inside nawk ??

Absolutely, only you need to separate the args with commas; slight difference in the syntax between ksh and (|n|g)awk...

Ok..

printing spaces, gives me a problem,but i figured a way out is that legitimate ?
This is how am printing using printf in UNIX

 
printf "%-1s%-2s%-4s%-30s%-15s%-12s%-15s              %015.2f%c%015.2f%c%c   %-3s         %-1c\n" "0" $H1 $H2 $F_D $S_S $D_P $S_P $CT $P $DT $M $W $CMP "X" >> New

Output is also as expected

 
cat New
0122010KKPHARMA_KKE                  KKE_BRIDGE     COMPETRED   CMPPP-GL                     000012099415.79+000011898578.29-W   140         X
 

But is there a way to print spaces with harding inside a variable like

SP=" "
and use the above SP in printf ?

Ofcourse the $F_D="KKPHARMA_KKE " works absolutly fine but not SP=" "

How do i print aplha numeric characters with zeros padded in front? is it possible? using printf !:wall:

Yes. I'd start you off w a hint such as: %035.35s.

However, the most effective approach for you overall is to scrounge up a tutorial that covers printf. Really the same features and behavior throughout pretty much all languages, shells, environments but mostly syntactic nuances to watch out for.

1 Like