?
Solved

Format monetary amounts in AWK

Posted on 2003-03-14
15
Medium Priority
?
422 Views
Last Modified: 2013-12-26
Does anyone know a function to format monetary amounts in Awk programming? I would like the output to be something like $1,234,567.89.
I know there is a strfmon() function to convert monetary values to string.  But, as far as I know, I can only use this function in C.
Is there a way to use the same function in AWK?  Or is there another way of doing it in AWK?
Thanks for your help.
0
Comment
Question by:pauloduarte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +4
15 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8136366
This should be in AWK?
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8136382
I have perl solution


----------------
Input file
----------------
1510 bin/ee> cat h1
20200.45500
232334123.3234

----------------
Output file
----------------
1511 bin/ee> cat h1.out
20,200.45500
232,334,123.3234

----------------
Perl Source code
----------------
#!/usr/bin/perl

#----------------------
# Sub to add comma's
#----------------------
sub commify {
  local $_  = shift;
  1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
  return $_;
}

#----------------------
# Define input file
#----------------------
$sourceFile="h1";

#----------------------
# Define output files
#----------------------
$DATAFile="h1.out";

#----------------------
# Create File Handlers
#----------------------
open(IN,"$sourceFile");
open(OUTA,">$DATAFile");

while ($myline=<IN>){
   print OUTA commify($myline) ;
}

close(IN);
close(OUTA);

0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8136650
in case you have a shell script and you want to call perl script with an argument to return money formated number

then you need the following perl script

1794 bin/ee> cat ccy_con.pl
#!/usr/bin/perl
#----------------------
# Sub to add comma's
#----------------------
sub commify {
   local $_  = shift;
   1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
   return $_;
}
$arg_one= $ARGV[0];  
print commify($arg_one) ;


To call it from command prompt
bin/ee> echo Convert 123456.55 using perl `ccy_con.pl 123456.55`
Convert 123456.55 using perl 123,456.55

bin/ee> echo Convert 123456789.55 using perl `ccy_con.pl 123456789.55`
Convert 123456789.55 using perl 123,456,789.55

To call it from shell script

my_number=123456789.55
parsed_number=`ccy_con.pl $mynumber`

note that
` is back quote
0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8136698
extra line to display "$"


1794 bin/ee> cat ccy_con.pl
#!/usr/bin/perl
#----------------------
# Sub to add comma's
#----------------------
sub commify {
  local $_  = shift;
  1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
  return $_;
}
$arg_one= $ARGV[0];  
print "\$" ;
print commify($arg_one) ;

0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8136779
Perl_Experts:
Yes, we could use
     $x = "\$" . commify($ARGV[0]) ;
     print $x ;
instead of
     $arg_one= $ARGV[0];  
     print "\$" ;
     print commify($arg_one) ;

I am just learning the wonderful world of Perl.
0
 

Author Comment

by:pauloduarte
ID: 8137474
Hassan, this should be under AWK, but there is no AWK programming link.  Regarding your PERL answer, unfortunately it is not going to work for me.  I need an answer in AWK.  Thanks anyway.
0
 

Author Comment

by:pauloduarte
ID: 8137732
I have created my own AWK function to display monetary amounts. Thanks.
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8138755
pauloduarte,

mind posting it?  Thanks.
0
 
LVL 3

Expert Comment

by:rfr1tz
ID: 8140561
See HamdyHassan, everyone prefers AWK over perl :)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8142208
roughly this way:
echo 1234567.89 | awk -F. '{i=split($1,a,//);x=1;while(i>0){if (i%3 == 0){printf","}printf"%d",a[x];i--;x++}print"."$2}'
0
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8144456
"See HamdyHassan, everyone prefers AWK over perl :) "
OZO prefer perl !!!!
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8144696
agree, much simpler in perl
  echo 1234567.89 | perl -F'\.' -lane '$x=reverse $F[0];$x=~s/([0-9]{3})/$1,/g;$x=reverse $x;print "$x.$F[1]"'
(and gets shorter if ozo joins;-)
0
 

Accepted Solution

by:
mkadwa earned 300 total points
ID: 8389524
ahoffman, your awk script does not work.! Splitting on null causes unpredictable results. And...if the split DID work, you would get 123,456,7.89 !

how about this in AWK!!! :

echo "1123456789.69"|awk -F\. '{x=length($1)%3;if(x==0)x=3;s="$ "substr($1,1,x);for(i=1+x;i<=length($1);i+=3){s=s "," substr($1,i,3)}s=s"."$2;print s}'

Try it with 123 instead of 1123 ;-)
Chow
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8389631
dooh, trapped into the incompatibilities of awk vs. gawk, even nawk fails :-(
mkadwa, thanks for the hint
0
 
LVL 18

Expert Comment

by:liddler
ID: 9845918
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by mkadwa

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

liddler
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question