Sum Dollar Amounts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sum Dollar Amounts
# 1  
Old 09-09-2005
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 awk or something?


Any help would be appreciated.


Thanks.
# 2  
Old 09-09-2005
Do you have a sample of the input file containing the values.

Look at this sample file and script

Code:
$ cat sum.txt
12 23
23 34
34 45
45 56
$ awk '{ print $1+$2 }' sum.txt  
35
57
79
101

# 3  
Old 09-09-2005
Here is a sample

I don't have the sample file yet but it would be something like

Header Record
Indicator 'HR'
Date 'YYYYMMDD'
Version 1

Record Layout
acct no varchar2(10)
indicator varchar2(1)
amt number

Trailer
Indicator 'TR'
Total Rec cnt not includind header or trailer
Total Credits sume of the amount of credits
Total Debits sum of the amt of debits


Sample file

HR|20050909|1
1111111111|C|100.00
2222222222|D|200.00
3333333333|C|50.00
4444444444|D|10.00
TR|4|150.00|210.00


So the program should be able to come up with Total Credits = 150.00 and Total Debits = 110.00. The file I will be working with will have around 100,000 records. I want to compare my count to the trailer to verify the file contents.
# 4  
Old 09-09-2005
Hi
Just Try this

echo "Total Credit = " $( grep "\|C\|" YourFile| awk -F"|" '{ S = S + $3 }END{ print S }' )
echo "Total Credit = " $( grep "\|D\|" YourFile| awk -F"|" '{ S = S + $3 }END{ print S }' )


If the amount is very big , you will have to format the ouput .....
let me know the result..

Shihab
# 5  
Old 09-09-2005
In one command:

Code:
awk -F'|' '{ if ($2 == "C") { c += $3; } if ($2 == "D") { d += $3; } } END {printf ("Credit: %d\n",c); printf ("Debit: %d\n",d);}' inputfile.txt

# 6  
Old 09-09-2005
Thanks for the help.

Thank you.
# 7  
Old 09-09-2005
Unbeliever is correct. You don't need grep when you're using awk. Here's a shorter version of his program.

Code:
awk -F'|' '$2=="C"{c += $3} $2=="D"{d += $3} END{print "Credit:",c; print "Debit:",d}' inputfile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep and dollar command

Hello there, first of all, don't hesitate to notify me if I misbehave. I'm a newbe in the use of this site, and in unix too (in english as well :-) Well, if someone hat a little time to waste, could he explain to me this strange behavior. I'm using ksh under AIX. cdeset=$(su - db2inst1 -c... (8 Replies)
Discussion started by: skitoc
8 Replies

2. 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

3. 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

4. Shell Programming and Scripting

problem in dollar substitution

Hi, I have a problem in dollar substitution:- $ csv1="first_csv" $ csvnumber=1 $ echo {csv$csvnumber} {csv1} $ echo "${csv$csvnumber}" bad substitution I want first_csv...why is it not working thanks (2 Replies)
Discussion started by: scripter12
2 Replies

5. 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

6. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: sri77
4 Replies
Login or Register to Ask a Question