in reply to Calculate hourly averages
#!/usr/bin/perl use strict; use warnings; my %count; my %sum; while (<DATA>) { chomp; next if /^itemid/; my @F = split; my @J = split(/\:/,$F[2]); my $hour = $J[0]; if ( $J[0] =~ /[0-9][0-9]/ ){ $count{$hour}++; $sum{$hour} += $F[-1]; }; } for (sort keys %count ) { print "$_ ", $sum{$_} / $count{$_}, "\n"; } __DATA__ itemid FROM_UNIXTIME(clock) value 26603 2010-10-09 00:00:23 38784360.8136 26603 2010-10-09 00:01:23 36529742.6667 26603 2010-10-09 00:02:23 36966880.8000 26603 2010-10-09 00:03:23 35666405.8667 26603 2010-10-09 01:05:23 35600190.6667 26603 2010-10-09 02:06:23 39175029.2000 26603 2010-10-09 03:07:23 35386478.4000 26603 2010-10-09 04:08:27 35773482.7500 26603 2010-10-09 05:09:23 39073367.5714 26603 2010-10-09 06:10:23 34379049.8667 26603 2010-10-09 07:11:24 33984045.7705 26603 2010-10-09 09:12:23 36276301.8305 26603 2010-10-09 10:13:23 37248366.0000
If the logfile entries are always sorted by hour, you don't even need the hash:
use strict; use warnings; my $count = 0; my $sum = 0; my $previous_hour = -1; while (<DATA>) { chomp; next if /^itemid/; my @F = split; my @J = split(/\:/,$F[2]); my $hour = $J[0]; if ($hour != $previous_hour && $count) { print $previous_hour, " ", $sum / $count, "\n"; $sum = 0; $count = 0; } $sum += $F[-1]; $count++; $previous_hour = $hour; }
|
|---|