Visit Our UNIX and Linux User Community


change number into dollars and cents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting change number into dollars and cents
# 1  
Old 08-01-2011
change number into dollars and cents

in ksh.

Here is what I have working thanks to all of you. Now I need to take it one step further. The $result value needs to be in the $0000.00 format instead of what it is now, 000000. Yes, it is always 6 characters, I thought this would help but so far it has baffled me.

My thought was to pull the right two numbers into one variable and left 4 numbers into another, then concat them with a dollar sign in front. But I cannot get it to work right.

This is what I have that works right now:
Code:
 num=`grep "^X1" <filename|cut -c26-31`
 result=`echo $num | awk '$1=$1' OFS="+" | bc`
 
echo "Total Dollar amount is $result"


Last edited by Franklin52; 08-01-2011 at 11:02 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-01-2011
Forget the last code, try this instead:
Code:
result=`echo "$result" | sed 's/^/$/;s/..$/.&/'`


Last edited by bartus11; 08-01-2011 at 10:21 AM..
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-01-2011
Thanks for the printf, never used it before. The result is not what I need and I am guessing I just can't get it to format out as I want. the number is 23240 for example and it is printing out on the screen as 23240.00 instead of $232.40

---------- Post updated at 09:58 AM ---------- Previous update was at 09:41 AM ----------

I have always been hesitant to use the sed command, it has been a while since I was in a UNIX shop, been in windows for the last 8 years and now back in UNIX.

But the sed command is working. Thanks.
# 4  
Old 08-01-2011
Glad it worked Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #336
Difficulty: Easy
sed -e 'r' /etc/passwd will recover lost password on a Unix system.
True or False?

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2 dollars wrapped to a string

Hi, I am writing an awk script and I have got stuck. I am trying to find a word that has 2 dollar signs in it but its failing. I have already tried to escape it with single and double back slash and also I have tried to use \044 in the search but even that doesnt work. can you please help me... (7 Replies)
Discussion started by: hitmansilentass
7 Replies

2. UNIX for Dummies Questions & Answers

How do i change string in a certain column number?

I have a file which looks like this 01081023442 220000000410629994812279000001099952504280000000000000000000000000010050000000000 0000000000 0000000000 1101211221099952504280000010704525042800120000000000000000000000000000000000001 0000000000000006000000000000815500000010000010000000000000... (2 Replies)
Discussion started by: reignangel2003
2 Replies

3. Shell Programming and Scripting

Change the File name using pattern and number

Hi, i have the below files in the wrk folder. File names are AAB0001.R BBA0002.R CCDC0003.R AAB0002.R AAB0034.R BBA0081.R DDDE0008.R i need to change the file name USING pattern and number. output like below :wall: AAB0001.R AAB0002.R AAB0003.R BBA0001.R BBA0002.R CCDC0001.R... (3 Replies)
Discussion started by: krbala1985
3 Replies

4. Shell Programming and Scripting

Need to change format of number

Hi, using a shell script to get values from a CSV eg: 12345.67,5678990.89,76232882.90 12345,5678990.89,76232882 Need the format of these numbers to change to 12,345.67:5,678,990.89:76,232,882.90 12,345:5678990.89:76232882 Using nawk on solaris, to parse these values, need the... (10 Replies)
Discussion started by: pgop
10 Replies

5. Shell Programming and Scripting

how to change a number in a file with sed?

Hello, I have a file which contains some line as follows, 9.9 TEMP 9.9 MUCOEFF 0.0 EPSILON And I want to increase this MUCOEFF by 0.2 as 9.9 TEMP 10.1 MUCOEFF 0.0 ... (7 Replies)
Discussion started by: lorenzz
7 Replies

6. Shell Programming and Scripting

How to change name for N number of files

Hi Guys, Please can you let me know, how to change file names in a directory that has say 10 files I have files like STR1.rman.pre.script STR1.rman.post.script STR1.rman.inc.script STR1.rman.cold.script STR1.rman.hot.script I need a command to change the above files all in one go... (6 Replies)
Discussion started by: kamathg
6 Replies

Featured Tech Videos