Summing amounts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing amounts
# 1  
Old 12-23-2008
Summing amounts

Hi
I need to sum of sub total amounts. Please any ideas
Code:
file1.txt

type: xx

        xyz               200   300

        xyz1             300   400

Sub total               500   700

type:yy

        abc              200   300

        abc1            100   100

Sub total              500   700

I am expecting


type: xx

        xyz               200   300

        xyz1             300   400

Sub total               500   700

type:yy

        abc              200   300

        abc1            100   100

Sub total              300     400

Total                   800     1100      ---sum of sub totals

Thanks and Regards,
MR
# 2  
Old 12-24-2008
Hi,

take a look at your example. Your numbers don't add up correctly.

Anyway, try:

Code:
awk '/Sub total/{sum1=sum1+$NF; sum2=sum2+$(NF-1)}{print}END{print "Total: " sum2, sum1}' xxx

HTH Chris
# 3  
Old 12-24-2008
input:
Code:
type: xx

        xyz               200   300

        xyz1             300   400
type:yy

        abc              200   300

        abc1            100   100
type:zz

        ttt              200   300

        ttt1            800   700

output:

Code:
type: xx

        xyz               200   300

        xyz1             300   400
Sub Total		500		700
type:yy

        abc              200   300

        abc1            100   100
Sub Total		300		400
type:zz

        ttt              200   300

        ttt1            800   700        
Sub Total		1000		1000
Total		1800		2100

code:

Code:
$flag=1;
open FH,"<b.txt";
while(<FH>){
	if(m/type/){
		print "Sub Total		$sub_a		$sub_b\n" if ($.!=1);
		$flag++;
		$sub_a=0;
		$sub_b=0;
		print;
		next;
	}
	else{
			if (m/(.*?  *?)([0-9][0-9]*)(.*?)([0-9][0-9]*)(.*)/)
			{
				$a+=$2;
				$sub_a+=$2;
				$b+=$4;
				$sub_b+=$4;
			}
			print;
	}
}
print "\n";
print "Sub Total		$sub_a		$sub_b\n";
print "Total		$a		$b";

# 4  
Old 12-24-2008
I tried the below command,its not working.

Code:
$nawk '/sub total/{sum1=sum1+$NF; sum2=sum2+$(NF-1)}{print}END{print "Total: " sum2, sum1}' test.txt

Invoice Type: CM


INV_TYPE       MONTH        Invoice Count Sum Of Invoices
-------------- ------------ ------------- ---------------
CM             DEC                      1           1,000
               NOV                      1             100

sub total                             2           1,100

Invoice Type: CO


INV_TYPE       MONTH        Invoice Count Sum Of Invoices
-------------- ------------ ------------- ---------------
CO             AUG                      1           5,000

sub total                             1           5,000

Total: 3 6

Thanks,
MR
# 5  
Old 12-24-2008
What exactly is not working?
What exactly shall be summed up?
And the numbers in your new example are different from
your old example as they contain commas. Perhaps
you should provide us with a representative example?
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

summing from two different files

I have two files hhhh 3674.00 a 75 1535 183 2134 291 2452 442 2738 704 3048 a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610 a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985 ... (4 Replies)
Discussion started by: Indra2011
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

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

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

6. Shell Programming and Scripting

Summing on column

Hi Friends How to do sum on a column? I have a file like: FRED 500.01 TX SMITH 50.10 NY HARRY 5.00 CA 555.11 Sum on second column. I am trying using nawk like nawk 'BEGIN {FS="|"}; {printf $1"+"}' Thanks a lot for your help S :) (2 Replies)
Discussion started by: sbasetty
2 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