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

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

#! /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 ?

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

$ 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

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