For Programmers: Free Programming Magazines  


Home > Archive > AWK > June 2007 > group, sort and sum









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author group, sort and sum
Atropo

2007-06-19, 7:57 am

The following is the output of a command. I'd like sum the fourth
column grouped by the first.
...
event priority lock quantity

2010 0 1 1
2010 2 0 9
2012 2 0 2
2040 2 0 7
2010 3 0 1
2040 3 0 3
2010 4 0 1
2010 5 0 1
2032 98 0 7
2032 99 0 5
2037 99 0 3

...
on other file I have this.
....
2031------ Event_name@serverName
2032------ Event_name@serverName
2024------ Event_name@serverName
2022------ Event_name@serverName
2000------ Event_name@serverName
2010------ Event_name@serverName
3700------ Event_name@serverName
....
the key on both is the first column

none of those outputs are sorted.

the output I want is Event_name@serverName EventNumber
Quantity

TIA

Thomas Weidenfeller

2007-06-19, 9:57 pm

Atropo wrote:
> The following is the output of a command. I'd like sum the fourth
> column grouped by the first.
> ..
> event priority lock quantity
>
> 2010 0 1 1
> 2010 2 0 9
> 2012 2 0 2
> 2040 2 0 7
> 2010 3 0 1
> 2040 3 0 3
> 2010 4 0 1
> 2010 5 0 1
> 2032 98 0 7
> 2032 99 0 5
> 2037 99 0 3



> on other file I have this.
> ...
> 2031------ Event_name@serverName
> 2032------ Event_name@serverName
> 2024------ Event_name@serverName
> 2022------ Event_name@serverName
> 2000------ Event_name@serverName
> 2010------ Event_name@serverName
> 3700------ Event_name@serverName
> ...
> the key on both is the first column
>
> none of those outputs are sorted.
>
> the output I want is Event_name@serverName EventNumber
> Quantity


$ awk '
FILENAME == ARGV[1] && FNR > 2 {
quantities[$1] += $4
next
}
FILENAME == ARGV[2] {
e = $1
sub(/--*/, "", e)
names[e] = $2
next
}
END {
for(e in quantities) {
print names[e] " " e " " quantities[e]
delete names[e]
}
for(e in names) {
print names[e] " " e " 0"
}
} ' command_output_file other_file


/Thomas
Ed Morton

2007-06-20, 9:57 pm

Atropo wrote:

> The following is the output of a command. I'd like sum the fourth
> column grouped by the first.
> ..
> event priority lock quantity
>
> 2010 0 1 1
> 2010 2 0 9
> 2012 2 0 2
> 2040 2 0 7
> 2010 3 0 1
> 2040 3 0 3
> 2010 4 0 1
> 2010 5 0 1
> 2032 98 0 7
> 2032 99 0 5
> 2037 99 0 3
>
> ..
> on other file I have this.
> ...
> 2031------ Event_name@serverName
> 2032------ Event_name@serverName
> 2024------ Event_name@serverName
> 2022------ Event_name@serverName
> 2000------ Event_name@serverName
> 2010------ Event_name@serverName
> 3700------ Event_name@serverName
> ...
> the key on both is the first column
>
> none of those outputs are sorted.
>
> the output I want is Event_name@serverName EventNumber
> Quantity
>
> TIA
>


awk 'NR<3{next} {key=$1+0} NR==FNR{sum[key]+=$4; name[key]="noname";
next} {name[key]=$2}
END{for (key in name) printf "%s %s %d\n",name[key],key,sum[key]}' file1
file2

Ed.
Chocolateman

2007-06-27, 12:05 pm

http://www.thetubebender.com/Play?clip=1673286
Desert

2007-06-30, 8:34 pm

Olsen Twins Shows Butt & Tiny Tits At Home!

http://www.yourtubeaudio.com/PlayMo...i?movie=1673286
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com