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