Sponsored Content
Top Forums Shell Programming and Scripting Korn Shell manipulating the string into dynamic currency number Post 302944339 by Don Cragun on Tuesday 19th of May 2015 12:47:55 AM
Old 05-19-2015
What operating system are you using? What version of the Korn shell are you using?

On OS X, with ksh version sh (AT&T Research) 93u+ 2012-08-01, the following script (using the variables you specified in your 1st post):
Code:
#!/bin/ksh
for number_of_positions in 2 4
do	for input_string in 345987 1345987
	do	output_string=$(printf "%'.*f\n" $number_of_positions \
			$input_string)
		printf 'number_of_positions=%d\n' $number_of_positions
		printf 'input_string=%s\n' $input_string
		printf 'Output=%s\n\n' "$output_string"
	done
done

produces the output:
Code:
number_of_positions=2
input_string=345987
Output=345,987.00

number_of_positions=2
input_string=1345987
Output=1,345,987.00

number_of_positions=4
input_string=345987
Output=345,987.0000

number_of_positions=4
input_string=1345987
Output=1,345,987.0000

And, it produces the same output with or without the zero in the printf format string before the decimal point.

PS. In what way is ksh failing? What diagnostic does it print?

Last edited by Don Cragun; 05-19-2015 at 02:08 AM.. Reason: Add PS.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
LOCALECONV(3)						   BSD Library Functions Manual 					     LOCALECONV(3)

NAME
localeconv -- natural language formatting for C LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <locale.h> struct lconv * localeconv(void); #include <xlocale.h> struct lconv * localeconv_l(locale_t locale); DESCRIPTION
The localeconv() function returns a pointer to a structure which provides parameters for formatting numbers, especially currency values: struct lconv { char *decimal_point; char *thousands_sep; char *grouping; char *int_curr_symbol; char *currency_symbol; char *mon_decimal_point; char *mon_thousands_sep; char *mon_grouping; char *positive_sign; char *negative_sign; char int_frac_digits; char frac_digits; char p_cs_precedes; char p_sep_by_space; char n_cs_precedes; char n_sep_by_space; char p_sign_posn; char n_sign_posn; char int_p_cs_precedes; char int_n_cs_precedes; char int_p_sep_by_space; char int_n_sep_by_space; char int_p_sign_posn; char int_n_sign_posn; }; The individual fields have the following meanings: decimal_point The decimal point character, except for currency values, cannot be an empty string. thousands_sep The separator between groups of digits before the decimal point, except for currency values. grouping The sizes of the groups of digits, except for currency values. This is a pointer to a vector of integers, each of size char, representing group size from low order digit groups to high order (right to left). The list may be terminated with 0 or CHAR_MAX. If the list is terminated with 0, the last group size before the 0 is repeated to account for all the dig- its. If the list is terminated with CHAR_MAX, no more grouping is performed. int_curr_symbol The standardized international currency symbol. currency_symbol The local currency symbol. mon_decimal_point The decimal point character for currency values. mon_thousands_sep The separator for digit groups in currency values. mon_grouping Like grouping but for currency values. positive_sign The character used to denote nonnegative currency values, usually the empty string. negative_sign The character used to denote negative currency values, usually a minus sign. int_frac_digits The number of digits after the decimal point in an international-style currency value. frac_digits The number of digits after the decimal point in the local style for currency values. p_cs_precedes 1 if the currency symbol precedes the currency value for nonnegative values, 0 if it follows. p_sep_by_space 1 if a space is inserted between the currency symbol and the currency value for nonnegative values, 0 otherwise. n_cs_precedes Like p_cs_precedes but for negative values. n_sep_by_space Like p_sep_by_space but for negative values. p_sign_posn The location of the positive_sign with respect to a nonnegative quantity and the currency_symbol, coded as follows: 0 Parentheses around the entire string. 1 Before the string. 2 After the string. 3 Just before currency_symbol. 4 Just after currency_symbol. n_sign_posn Like p_sign_posn but for negative currency values. int_p_cs_precedes Same as p_cs_precedes, but for internationally formatted monetary quantities. int_n_cs_precedes Same as n_cs_precedes, but for internationally formatted monetary quantities. int_p_sep_by_space Same as p_sep_by_space, but for internationally formatted monetary quantities. int_n_sep_by_space Same as n_sep_by_space, but for internationally formatted monetary quantities. int_p_sign_posn Same as p_sign_posn, but for internationally formatted monetary quantities. int_n_sign_posn Same as n_sign_posn, but for internationally formatted monetary quantities. Unless mentioned above, an empty string as a value for a field indicates a zero length result or a value that is not in the current locale. A CHAR_MAX result similarly denotes an unavailable value. The localeconv_l() function takes an explicit locale parameter. For more information, see xlocale(3). RETURN VALUES
The localeconv() function returns a pointer to a static object which may be altered by later calls to setlocale(3) or localeconv(). The return value for localeconv_l() is stored with the locale. It will remain valid until a subsequent call to freelocale(3). If a thread-local locale is in effect then the return value from localeconv() will remain valid until the locale is destroyed. ERRORS
No errors are defined. SEE ALSO
setlocale(3), strfmon(3) STANDARDS
The localeconv() function conforms to ISO/IEC 9899:1999 (``ISO C99''). HISTORY
The localeconv() function first appeared in 4.4BSD. BSD
November 21, 2003 BSD
All times are GMT -4. The time now is 07:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy