Text Formatting using printf[ksh]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text Formatting using printf[ksh]
# 1  
Old 03-19-2012
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
Code:
a='                      '
b='          '
c='          '
d='          '

Sometimes the variable might contain values and sometimes they are blank. But when the variables do not have values it doesn't work properly.
Code:
I have tried this script:
printf "%22s%s%10f%s%10f%s%10f%s" "$a" "~" "$b" "~" "$c" "~" "$d" "~" >>res.dat
 
And also:
printf "%22s%s%10f%s%10f%s%10f%s" $a "~" $b "~" $c "~" $d "~" >>res.dat

But nothing works. Can anyone fix the above statement.
Thanks!!
# 2  
Old 03-19-2012
In what manner does it "not work"?

You don't need to initialize the variables to anything, especially since that may end up making them longer than you want.

%10f won't do what you want either. Treat them all as strings.

I'd make the ~ part of the format string so you don't need to put int 9 identical options to separate 10 fields.

You forgot to put a \n on the end.

Code:
printf "%22s~%10s~%10s~%10s~\n" a 3.14159 a b c

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-19-2012
If a variable is blank, It gives an error < Specify a number instead of '~' >
# 4  
Old 03-19-2012
Put the variable in quotes. "$a"
# 5  
Old 03-19-2012
Hi Corona,

have done the same in my first printf statment, but it isn't working.shows the same error.
# 6  
Old 03-19-2012
Show exactly what you're doing, word for word, letter for letter, keystroke for keystroke, please.

%10f makes no more sense now than it did then, so do correct your statement as well...

Last edited by Corona688; 03-19-2012 at 07:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing printf formatting parameters as variables

Hi, I haven't programed in C in a few years. I have been doing a lot of shell scripting, I.E. not really programming anything heavy. :o That said, I have a script that gives hourly usage statistics for our email server. It runs w-a-y to slow as a script for my impatience, and needs to... (7 Replies)
Discussion started by: mph
7 Replies

2. Shell Programming and Scripting

Problem with printf in UNIX KSH shell

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... (6 Replies)
Discussion started by: selvankj
6 Replies

3. 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

4. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

5. UNIX for Dummies Questions & Answers

printf formatting

Is there a way to make these 2 numbers - $482477.37 and $1875000.00 look like $482,477.37 and $1,875,000.00 with printf? (4 Replies)
Discussion started by: nickg
4 Replies

6. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

7. Shell Programming and Scripting

formatting date in ksh

hi all, in ksh, how do i format date so it includes hour and minute ?? i am trying the following command : date +%Om/%Od/%Oy%OH:%M but it displays the hour and minute concatenated with the day/month/year e.g 12/10/0814:08 when i want the output to be 12/10/08 14:08 i tried... (4 Replies)
Discussion started by: cesarNZ
4 Replies

8. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

9. 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

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