Help on formatting a number in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help on formatting a number in ksh
# 1  
Old 05-26-2002
Help on formatting a number in ksh

I don't know if this is possible. Still fairly new to ksh scripting. Here is what I am wanting to do with my output.

change the following

From:

1349870987

To:

1,349,870,987

in my output. How do I do this?

Thanks,
S
# 2  
Old 05-26-2002
Code:
#! /usr/bin/ksh
i1=1349870987
echo $i1
typeset -Z12 i2=$i1
echo $i2
i3=$(echo $i2 | sed 's/\(...\)/,\1/g')
echo $i3
i4=${i3##+([0,])}
echo $i4
exit 0

i1 is the original number.

i2 has been formatted to have leading zeros. The 12 was arbitrary, but it must be larger than the number of digits in the number and it must be a multiple of 3.

i3 results by using sed to prepend a comma to every 3 digits in the number.

i4 is the final result where we have removed leading commas and zeroes.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh file formatting

Hi. If I have a txt file in the following format: option1=123 option2=abc option3=321 How do I convert it with KSH into the following format (with the additional prefixed string): ${filename},::option1=123,option2=abc,option3=321 Many thanks (3 Replies)
Discussion started by: user052009
3 Replies

2. Shell Programming and Scripting

Formatting output using awk in ksh

:oi was trying to write a script to format output of a command in ksh which has output as below: so i used : to get which i require at all times. But problem occurs when status part changes. above output i get when status is SU (success).If the status is IN (inactive), output of... (1 Reply)
Discussion started by: pr5439
1 Replies

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

4. Shell Programming and Scripting

Problem in formatting number

Hi, I was trying to format my number like i=1 to 000001 using the below method. typeset -Z6 i (sorry, corrected) My shell is K, is not doing, it is supposed to do Thanks in advance (6 Replies)
Discussion started by: ezee
6 Replies

5. Shell Programming and Scripting

Increment a floating number in ksh

Hi ! How to increment a varibale in ksh. #!/bin/ksh set -x RELEASE_NUM=5.2.103 VAL=0.0.1 RELEASE_NUM=`echo $RELEASE_NUM + $VAL | bc` echo $RELEASE_NUM The above code is throwing this error. + RELEASE_NUM=5.2.103 (2 Replies)
Discussion started by: dashok.83
2 Replies

6. Shell Programming and Scripting

ksh: random number between 1-10

How do I create a random number between 1 and 10 in kornshell? (2 Replies)
Discussion started by: dangral
2 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

Grep a number from a line in ksh

In file.name, I have a line that reads $IDIR/imgen -usemonths -dropcheck -monitor -sizelimit 80000000 -interval 120 -volcal HSI How can I get the size limit, i.e. 80000000 out and pass it to a variable called SIZE? Thanks. I tried echo "grep sizelimit file.name" | sed -n -e... (3 Replies)
Discussion started by: rodluo
3 Replies

9. UNIX for Advanced & Expert Users

formatting textfile inside ksh script using awk not working

I cannot seem to get this text file to format. Its as if the awk statement is being treated as a simple cat command. I manned awk and it was very confusing. I viewed previous posts on this board and I got the same results as with the the awk command statement shown here. Please help. ... (6 Replies)
Discussion started by: tekline
6 Replies

10. Shell Programming and Scripting

How to format number/string in ksh

Hi, How to format a number in ksh. For example x=RANDOM $$ I want x to be of 20 digits long, so if x = 12345 I want it to be left paded with 15 zeros. Thanks. (2 Replies)
Discussion started by: GNMIKE
2 Replies
Login or Register to Ask a Question