Formatting amounts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting amounts
# 1  
Old 06-25-2008
Formatting amounts

Hi,

I am exporting data from a table with 4 columns into a file. The 4th column contains amount which is decimal(11,2) with values ranging from 0.00 to 999999999.99. I need to mail this file to the users but before doing that i need to format the amount column as 999,999.00 instead of 999999.00

ex: input file
ab1,1234,gter,100000.00
ab2,1234,gter,500.00
ab3,1234,gter,999999999.99

output file should be
ab1,1234,gter,100,000.00
ab2,1234,gter,500.00
ab3,1234,gter,999,999,999.99


Thanks in advance
Sri
# 2  
Old 06-30-2008
It may be very tough for pure shell script, but not difficult for other dynamic languages such as Tcl:

An implementation in Tcl:
commify procedure is done by:
ASPN : Tcl Cookbook : Inserts thousand separators into a number
Code:
#! /usr/bin/tclsh

if { $argc != 1 } {
        puts stderr "Usage: $argv0 <input-file>"
        exit 1
}
set inputfile [lindex $argv 0]
if { [file exists $inputfile] == 0 } {
        puts stderr "Error. $inputfile does not exist"
        exit 2
}

proc commify {num {sep ,}} {
    while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}
    return $num
}


set fp [open $inputfile r]
while { [gets $fp line] >= 0 } {
        foreach { a b c d } [split $line ,] {}
        puts "$a,$b,$c,[commify $d]"
}
close $fp

BTW, will the commify output mess up the CSV output ?

Last edited by chihung; 06-30-2008 at 12:33 AM..
# 3  
Old 06-30-2008
Code:
awk 'BEGIN{FS=","}
 function cfmt(n  , s) {
   s = (ThouSep ? ThouSep : ",") "&"  # US convention the default
   while ( n ~ /^ *[-+(]? *[0-9][0-9][0-9][0-9]/ )
     sub ( /[0-9][0-9][0-9]([^0-9].*|$)/, s, n )
   return n
 } 
{
  print cfmt($4)      
}
' file

reference: here
# 4  
Old 06-30-2008
Code:
$ echo 1234567890 | sed -e :a -e 's/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
1,234,567,890
$ echo 12345678.90 | sed -e :a -e 's/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
12,345,678.90

# 5  
Old 06-30-2008
Hi.

There are more than 100 results in various languages for the common [pun intended] word for this operation, commify, at Koders Code Search: commify

They seem to be Windows-centric, I didn't notice awk or shell listed, but lots of tcl, Ruby, perl, PHP, Java, etc. ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to separate data with varying amounts of underscores?

All, I've searched and haven't found a solution for my particular issue. I have a file with lines of data that contain varying amounts of underscores imbedded. But all the strings have a final underscore and an interface name: dmk_hcn_dalian2.XXXX.XXX.XX.COM_Se0/0/0... (4 Replies)
Discussion started by: turk22
4 Replies

2. Shell Programming and Scripting

Help with formatting

Hi, I am new to UNIX and need your help in formatting the below input command to the desire output Input: CREATE UNIQUE INDEX XPKTABLE1 ( COL1, COL2 ) ON TABLE_NM; Output: COMMENT ON TABLE DB_NM.TABLE_NM AS 'PK=,COL1,COL2; '; In... (14 Replies)
Discussion started by: varun2327
14 Replies

3. Shell Programming and Scripting

Formatting

Good day All, I have requirement where my input data looks like below ] Message5 Expecting Output as 04/MAR/2104 ||| 23:15:45 ||| servername ||| NOTIFICATION |||message1||||||userId|||||| Message5 I could not use space delimiter as in the messages I would be having them as... (2 Replies)
Discussion started by: Tomlight
2 Replies

4. UNIX for Dummies Questions & Answers

XML / XSD: what is a good format for amounts?

Suppose I have a XSD definition of amount: <xs:element name="Amount" type="amount> And the amounts themselves are given in the following format in the XML: <amount>1.000.00</amount> In other words, the thousand separator and the decimal point are the same. Does this require that the parser... (3 Replies)
Discussion started by: figaro
3 Replies

5. Shell Programming and Scripting

Compare 2 folders to find several missing files among huge amounts of files.

Hi, all: I've got two folders, say, "folder1" and "folder2". Under each, there are thousands of files. It's quite obvious that there are some files missing in each. I just would like to find them. I believe this can be done by "diff" command. However, if I change the above question a... (1 Reply)
Discussion started by: jiapei100
1 Replies

6. Shell Programming and Scripting

Summing amounts

Hi I need to sum of sub total amounts. Please any ideas file1.txt type: xx xyz 200 300 xyz1 300 400 Sub total 500 700 type:yy abc 200 300 abc1 100 100 Sub total ... (4 Replies)
Discussion started by: mohan705
4 Replies

7. Shell Programming and Scripting

Sum Dollar Amounts

I get a transaction file and I need to sum two of the columns. I each record I get a debit(D) or credit(C) indicator. Then I get an amount field. I need to sum the total dollar value of debits and credits. I know I can loop through each record and get the sum total but is there a better way with... (7 Replies)
Discussion started by: lesstjm
7 Replies
Login or Register to Ask a Question