?
Solved

Format monetary amounts in AWK

Posted on 2003-03-14
15
Medium Priority
?
429 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: 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: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

569 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