Problem with printf in UNIX KSH shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with printf in UNIX KSH shell
# 1  
Old 07-10-2011
Problem with printf in UNIX KSH shell

Hi ALL,

I am using
Code:
SunOS 5.9 and KSH(bin/ksh)

The problem am facing is

Quote:
v1="12099415.79"
v2="11898578.29"
v3="+"
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
Quote:
printf ("%-9d%-1d%-9d" $v1 $v3 $v2)
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...
# 2  
Old 07-10-2011
Try:
Code:
printf "%015.2f%c%015.2f\n" $v1 $v3 $v2

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-10-2011
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:

Code:
~ # 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:

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

This User Gave Thanks to curleb For This Post:
# 4  
Old 07-11-2011
Thank you all...
One more question..
will it be possible to use the same printf formatting inside nawk ??
# 5  
Old 07-11-2011
Absolutely, only you need to separate the args with commas; slight difference in the syntax between ksh and (|n|g)awk...
# 6  
Old 07-11-2011
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

Code:
 
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
Code:
 
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 !Smilie
# 7  
Old 07-11-2011
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.
This User Gave Thanks to curleb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem running plsql using printf command on bash shell

I am running plsql using printf on a shell, but i am getting some strange error, can someone point what exactly am i missing, $ echo $SHELL /bin/bash $ printf " > SET serveroutput ON trimspool on feed off echo off > declare > p_val number; > d_val varchar2(10); > begin > SELECT... (1 Reply)
Discussion started by: kamauv234
1 Replies

2. Shell Programming and Scripting

Execute a shell script in UNIX m/c from win 7 using ksh

I need to run a shell script on unix machine from windows 7. I used the "RSH" command in win XP successfully for this, But on win 7 it is not working. Can any body tell me how to run RSH on win 7 or any other command for similar use. I use the command as below rsh <unix m/c> -l <username>... (1 Reply)
Discussion started by: arup1980
1 Replies

3. Ubuntu

How to lock a file through UNIX KSH shell script?

I wrote two shell scripts in UNIX that renames the same file and scheduled them at the same time. The following are the steps that I followed:- 1. I wrote 2 scripts named s1.sh and s2.sh, both trying to add “exec_” prefix to the name of the files present in a folder i which already don't start... (4 Replies)
Discussion started by: piuli
4 Replies

4. Shell Programming and Scripting

Text Formatting using printf[ksh]

Hi All, I am attempting to create a fixed length tilde delimited file using printf. The variables are initialized to fixed length blank spaces a=' ' b=' ' c=' ' d=' ' Sometimes the variable might contain values and sometimes they are... (5 Replies)
Discussion started by: angie1234
5 Replies

5. Shell Programming and Scripting

\n in ksh using echo & printf

#!/usr/bin/ksh var1="Hi World" var2="Morning" var3=$(echo "$var1" \n "$var2") echo $var3 var3=$(printf "$var1 \n $var2") echo $var3 Output Any way to get in my $var3 ? (7 Replies)
Discussion started by: dahlia84
7 Replies

6. Shell Programming and Scripting

problem with printf in shell script

i have written small script as follows: name="hi hello" printf "%-20s" $name This gives me strange output. -20s format is applied on both word of string. i.e it displays both word hi and hello in space of 20 length. I want to display entire string "hi hello" in length of 20 space. plz... (2 Replies)
Discussion started by: admc123
2 Replies

7. Shell Programming and Scripting

newbie: writing ksh shell problem

my default profile is using ksh, I tried to write a simple scripts and I had issues, below is my scripts: $ more if_num.ksh USAGE="usage: if_num.ksh" print -n "Enter two numbers: " read x y if ((x=y)) then print "You entered the same number twice." when I tried to executed the... (6 Replies)
Discussion started by: matthew00
6 Replies

8. Shell Programming and Scripting

KSH printf: columns and colors

Ih all, I need to make a ksh script with colors, it is possible with printf to combine column and colors ? i seem not working, I think i dont doing the good thing: printf -n "%-15s %-20s %-20s\n" "\033 the position is ok Name______Age________Site ----________---_________---- Bob... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. UNIX for Advanced & Expert Users

find command from shell scipt (ksh) problem

Hi experts, I have a simple shell script as follows. #!/bin/ksh FIND_STRING="\( -name 'testfile*.Z' -o -name 'DUMMY_*' \) " find /tmp -type f $FIND_STRING -print When I run this with ksh -x testscript, I get the following output. + FIND_STRING=\( -name 'testfile*.Z' -o -name... (6 Replies)
Discussion started by: kodermanna
6 Replies

10. Shell Programming and Scripting

printf command in ksh

Hi, I am very confused with my printf command. Somehow one variable can't line up with others... newstart2 ="Mon Nov 11 01 00:00:00 2002" printf "%-20s" $newstart2 Here is the output: Mon Nov 11 01 00:00:00 2002 It spread out to two lines.. Why? (1 Reply)
Discussion started by: cin2000
1 Replies
Login or Register to Ask a Question