From: rbalasus on
Calculations in .csv via SED AWK

Hello is there a way to make easy calculations in .csv files through
sed awk?

In my case I want to do the following:
date;cat;amount
01.01.2010;CAT1;60
01.01.2010;CAT1;5
01.01.2010;CAT2;7
01.01.2010;CAT2;50
01.02.2010;CAT2;1
01.02.2010;CAT2;2

I need a summarization and grouping by month.

month;cat;sumamount
01;CAT1;65
01;CAT2;57
01;CAT2;3

maybe there is a way to process this step by step in a loop, but for
me is interesting if there is a way to use some basic lowlevel tools
for this task.

Regards Randolf Balasus
From: Bill Marcum on
["Followup-To:" header set to comp.lang.awk.]
On 2010-03-03, rbalasus(a)gmail.com <rbalasus(a)gmail.com> wrote:
> Calculations in .csv via SED AWK
>
> Hello is there a way to make easy calculations in .csv files through
> sed awk?
>
> In my case I want to do the following:
> date;cat;amount
> 01.01.2010;CAT1;60
> 01.01.2010;CAT1;5
> 01.01.2010;CAT2;7
> 01.01.2010;CAT2;50
> 01.02.2010;CAT2;1
> 01.02.2010;CAT2;2
>
> I need a summarization and grouping by month.
>
awk -F ';' 'BEGIN{SUBSEP=";"} {sub(/\..*/,"",$1);sum[$1,$2]+=$3} \
END{for(x in sum)print x ";" sum[x]}'

> month;cat;sumamount
> 01;CAT1;65
> 01;CAT2;57
> 01;CAT2;3
>
> maybe there is a way to process this step by step in a loop, but for
> me is interesting if there is a way to use some basic lowlevel tools
> for this task.
>
> Regards Randolf Balasus
From: Kenny McCormack on
In article <0ce56197-bce5-40db-8b68-dc3863396285(a)z4g2000yqa.googlegroups.com>,
rbalasus(a)gmail.com <rbalasus(a)gmail.com> wrote:
>Calculations in .csv via SED AWK

What is "sed awk"? Is that some new thing from GNU?

From: Ed Morton on
On 3/3/2010 12:18 PM, rbalasus(a)gmail.com wrote:
> Calculations in .csv via SED AWK
>
> Hello is there a way to make easy calculations in .csv files through
> sed awk?
>
> In my case I want to do the following:
> date;cat;amount
> 01.01.2010;CAT1;60
> 01.01.2010;CAT1;5
> 01.01.2010;CAT2;7
> 01.01.2010;CAT2;50
> 01.02.2010;CAT2;1
> 01.02.2010;CAT2;2
>
> I need a summarization and grouping by month.
>
> month;cat;sumamount
> 01;CAT1;65
> 01;CAT2;57
> 01;CAT2;3

ITYM:

02;CAT2;3

>
> maybe there is a way to process this step by step in a loop, but for
> me is interesting if there is a way to use some basic lowlevel tools
> for this task.
>
> Regards Randolf Balasus

$ cat file
date;cat;amount
01.01.2010;CAT1;60
01.01.2010;CAT1;5
01.01.2010;CAT2;7
01.01.2010;CAT2;50
01.02.2010;CAT2;1
01.02.2010;CAT2;2

$ awk -F'[.;]' 'NR==1{print "month;cat;sumamount"; next}
{s[$2";"$4]+=$5} END{for (i in s) print i";"s[i]}' file
month;cat;sumamount
01;CAT1;65
01;CAT2;57
02;CAT2;3

Regards,

Ed.
From: rbalasus on

> What is "sed awk"?  Is that some new thing from GNU?
No I was talking about the tool "sed" and about the other tool "awk"
both are part of the coreutils in most installations.