Operating System - HP-UX
1755381 Members
3743 Online
108832 Solutions
New Discussion юеВ

summing columns using awk

 
SOLVED
Go to solution
Duncan Kerin
New Member

summing columns using awk

I have a text delimited file ("|") and I want to sum the 8th column. This is no problem when all the values are positive, however it does become a problem when a negetive value is encountered - it doesn't sum up the values correctly.

cat run15 | awk -F"|" -f a.awk > out

here's the a.awk file..
# Add the 8th column and print the sum
{ s += $8 }
END { print "sum of the total account balance is... ", s }

here's the input file, run15, ..
"5937000004 "|392953|"EN"|"O"|"prueba"|20030927|20020810|+0000005.09|20020815|936
"5937000011 "|393712|"SP"|"S"|"MARIA EUGENIA"|20011004||+0000000.82|20020712|5
"5937000012 "|393713|"EN"|"S"|"ANGEL IGNACIO"|20020815|20020818|+12.86|20020810|939
"5937000018 "|605963|"SP"|"S"|"OMAR LERTORA"|20020812|20020712|+0000013.56|20020304|943
"5937000028 "|605962|"EN"|"D"|"OMAR LERTORA"|20020313||-0000014.82|20020304|944
7 REPLIES 7
Deepak Extross
Honored Contributor
Solution

Re: summing columns using awk

Your awk script gives 17.51, which is correct. Or is it??
harry d brown jr
Honored Contributor

Re: summing columns using awk

I'm deeply confused:

+_5.09
+__.82
======
+_5.91
+12.86
======
+18.77
+13.56
======
+32.33
-14.82
======
+17.51

Is, there something we are missing??

because here' my sample run:

# cat run15 | awk -F"|" -f a.awk
sum of the total account balance is... 17.51
# cat a.awk
{ s += $8 }
END { print "sum of the total account balance is... ", s }
#



live free or die
harry
Live Free or Die
Ceesjan van Hattum
Esteemed Contributor

Re: summing columns using awk

Hi Duncan,
The calculations just seem to work normal. We do not see the problem.
awk 'BEGIN{FS="|"}{sum=sum+$8}END{print sum}' inputfile
.. it just works fine: 17.51 in your example.
Tell us more about the exact problem you might have..
Regards,
Ceesjan
harry d brown jr
Honored Contributor

Re: summing columns using awk


If you aren't getting the same results we got, then I suggest you install the latest patch bundle!

live free or die
harry
Live Free or Die
Duncan Kerin
New Member

Re: summing columns using awk

thanks guys, I've installed the latest patch bundle and I'm now getting same results.
harry d brown jr
Honored Contributor

Re: summing columns using awk


I'm glad it was just patches and not our brains...hey, is it Friday yet?

live free or die
harry
Live Free or Die
Deepak Extross
Honored Contributor

Re: summing columns using awk

umm...Ken Lay, is that you?

:-D