Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Grouping and Summing fields using either Awk or Perl

0 views
Skip to first unread message

Marc

unread,
Nov 24, 2009, 12:52:42 PM11/24/09
to
Hi guys, basically I have a file format of


123456789,9.09,AAAA
231456788,0.99,BBBB
098798765,1.99,CCCC
098750109,9.68,BBBB
098709866,0.01,AAAA

And I need the out put to disregard the first field and just group the
3rd field and sum the amount(field 2) for each group ie

Code AAAA Amount 9.10
Code BBBB Amount 10.67
Code CCCC 1.99

Any help on this would be greatly appreciated

pk

unread,
Nov 24, 2009, 1:02:09 PM11/24/09
to
Marc wrote:

> Hi guys, basically I have a file format of
>
>
> 123456789,9.09,AAAA
> 231456788,0.99,BBBB
> 098798765,1.99,CCCC
> 098750109,9.68,BBBB
> 098709866,0.01,AAAA
>
> And I need the out put to disregard the first field and just group the
> 3rd field and sum the amount(field 2) for each group ie
>
> Code AAAA Amount 9.10
> Code BBBB Amount 10.67
> Code CCCC 1.99

Assuming you don't care about output ordering, you can do

awk -F, '{t[$3]+=$2}END{for(i in t)printf "Code %s Amount %.2f\n", i, t[i]}'

with GNU awk, you can even force ordering by code of the output by setting
the WHINY_USERS environmental variable (to any value).

Tim Menzies

unread,
Nov 28, 2009, 8:46:51 PM11/28/09
to
tim menzies here (webmaster for awk.info). i'd be really interested in
talking direct to the mysterious "pk" about finding and posting useful
awk libraries.

mr/mrs/ms pk has 300+ posts to comp.lang.awk so he/her/it has much to
say that would be good to hear.

but pk's email address ends in "invalid" so i'm guessing if i email
him/her/it then that post will not go anywhere.

on the condition that i never broadcast his/her/it's email, would "pk"
care to contact me directly either at ma...@awk.info or t...@menzies.us?

thanks!

t

0 new messages