Korn Shell manipulating the string into dynamic currency number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell manipulating the string into dynamic currency number
# 8  
Old 05-19-2015
Sorry!! edited my last reply bit late..

Here is the same information..

OS version - AIX version 6.1
Korn Shell version - Version M-11/16/88f
# 9  
Old 05-19-2015
I believe AIX comes with both ksh and ksh93. Try using ksh93 instead of ksh. Also, if you can't use ksh93, try /usr/bin/printf in the ksh script instead of just printf when you have ' as a flag in your printf format strings.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 05-19-2015
same issue.. used /usr/bin/printf instead of printf
Code:
+ + /usr/bin/printf %'.*f\n 2 345987
printf: bad conversion
printf: bad conversion
output_string=.*f
.*f
+ printf number_of_positions=%d\n 2
number_of_positions=2
+ printf input_string=%s\n 345987
input_string=345987
+ printf Output=%s\n\n .*f
.*f
Output=.*f
.*f


Moderator's Comments:
Mod Comment edit by bakunin: same here - code-tags, please!

Last edited by bakunin; 05-19-2015 at 06:18 AM..
# 11  
Old 05-19-2015
What about trying it with ksh93 instead of ksh?
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 05-19-2015
we have only ksh..

below is the work with hard coded value.. resulted good.. but can you use the below command into your code and try it.. because I'm not able to use your code with the below command.
Code:
export LC_NUMERIC=en_US
$ echo "a" | awk '{printf ("%'\''2.2f",10000.1717)}'

---------- Post updated at 07:47 PM ---------- Previous update was at 07:09 PM ----------

Done with it.
Code:
echo "" | awk -v v1="10" '{printf ("%'\''2.2f",v1)}'

Thanks for your help Don..

Last edited by rbatte1; 05-19-2015 at 01:15 PM.. Reason: to give more explanation. RBATTE1 replace COLOR & FONT tags with CODE tags.
# 13  
Old 05-19-2015
If you have a bunch of these to do in a loop, you might want something more like:
Code:
#!/bin/ksh
for number_of_positions in 2 4
do	for input_string in 345987 1345987
	do	printf "%s %s\n" $number_of_positions $input_string
	done
done | awk -v fmt="%'.*f\n" '
{	printf("\nDecimal Places=%s\nInput=%s\nOutput=", $1, $2)
	printf(fmt, $1, $2)
}'

I assume you won't need the 1st printf in your awk script, but with a setup like this you can pass the number of decimal places you want and your column 1 values in on separate lines and get a line of output for the corresponding column 2 value you're trying to format.

Just firing up awk once to process an entire column of data is a lot more efficient than invoking awk once for each value.

If for some reason you have to do awk for a single value, you can at least get rid of the pipeline by using:
Code:
#!/bin/ksh
for number_of_positions in 2 4
do	for input_string in 345987 1345987
	do	awk -v fmt="%'.${number_of_positions}f\n" -v v="$input_string" 'BEGIN{printf(fmt, v)}'
	done
done

This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 05-19-2015
Tried with both the codes mentioned.. output is not having commas whereas I'm expecting to be as currency field..

first code:

Code:
Decimal Places=2
Input=345987
Output=345987.00
Decimal Places=2
Input=1345987
Output=1345987.00
Decimal Places=4
Input=345987
Output=345987.0000
Decimal Places=4
Input=1345987
Output=1345987.0000


second code:

Code:
345987.00
1345987.00
345987.0000
1345987.0000


Last edited by Scrutinizer; 05-19-2015 at 11:38 PM.. Reason: adding output
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Korn shell - evaluating string gives wrong results due to limitations

Anyone ever seen this? Someone mentioned this the other day.... If you do, for instance, in korn shell, echo $(5.2+2.5), it gives the result of 6 regardless. Can't remember why but it was the limitation of the korn shell. (5 Replies)
Discussion started by: psychocandy
5 Replies

2. Shell Programming and Scripting

korn shell: check the content of a string of a variable

hello, i have a variable which should have following content : var="value1" or var="value2" or var="value2:*" # example: value2:22 how can i check : - if the content is ok (value1 / value2* ) - the two options of "value2" when content is example "value2:22" , i want to split... (3 Replies)
Discussion started by: bora99
3 Replies

3. Shell Programming and Scripting

[C SHELL] How to pass dynamic number of arguments

I have a task. The scenario is like this. I have a operation program (OPR1) , whose function is to simply double the (single)value it receives as input. I have to write a script to operate the OPR1 and save its output in a file. Later, I have to modify the script so as to be able to operate ... (0 Replies)
Discussion started by: animesharma
0 Replies

4. Shell Programming and Scripting

String parsing in Korn Shell

Hi everybody, I have a string stored in a variable called record: record="SNMPv2-SMI::ent.9.9.43.1.3.9.2 = Timeticks: (177330898) 20 days, 12:35:08.98" I want to write some regular expressions good for Korn Shell to extract the number between parenthesis, in this case 177330898, and put it in... (3 Replies)
Discussion started by: omoyne
3 Replies

5. Shell Programming and Scripting

Korn Shell - Finding lowest number of a file.

I'm writing a KSH script that will get a file on the command line (such as input.txt), and in this file there is on number per line. The program needs to take the file, read each and determine the lowest number in the file. I currently have a while loop setup that will correctly out put every... (8 Replies)
Discussion started by: denyal
8 Replies

6. Shell Programming and Scripting

How to: Parse text string into variables using Korn shell

I am writing a script to keep check on free disk space, and I would like to find a way to parse $LINE (see code below) into a numeric value (for free disk space percentage) and a string value (for mount point). If possible, I would like to avoid sed or any additional use of awk since I am not very... (7 Replies)
Discussion started by: shew01
7 Replies

7. UNIX for Dummies Questions & Answers

string manipulating

I have a file like this: Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 6 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 2 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 5 With this command seira=`cut -f 2 -d ';' tes.txt` i take all the results (6,2,5 etc) and i store them in variable seira When i do... (9 Replies)
Discussion started by: psalas
9 Replies

8. Shell Programming and Scripting

Max number of parameters to korn shell?

Hi All, what is the maximum limit for the command line arguments in korn shell. Regards, Raju (4 Replies)
Discussion started by: rajus19
4 Replies

9. Shell Programming and Scripting

generate random number in korn shell

I want to be able to generate a random number within a korn shell script.. Preferably i would like to be able to state how many digits should be in this random number... ie 4 digits or 5 digits etc Any ideas? (2 Replies)
Discussion started by: frustrated1
2 Replies

10. Shell Programming and Scripting

shell to number and to string

Hello Does the unix korn shell provide a function to convert between number and string data-types regards Hrishy (1 Reply)
Discussion started by: xiamin
1 Replies
Login or Register to Ask a Question