#!/usr/bin/perl use strict; use warnings; use Date::Parse qw/ str2time /; use POSIX qw/ strftime /; chomp(my @line = split /,/, ); my @data = \@line; my $beg = str2time("@line[0,1]"); $beg = int($beg / 3600) * 3600; # truncate minutes and seconds my $end = $beg + 12 * 60 * 60; # add 12 hours printf "%s -+- %s\n", map {strftime "%Y-%m-%d %H:%M:%S", localtime $_} $beg,$end; while () { chomp(my @line = split /,/); my $date = str2time("@line[0,1]"); $date = int($date / 3600) * 3600; # truncate minutes and seconds if ($date < $end) { push @data, \@line; } else { process(@data); @data = \@line; $end = $date + 12 * 60 * 60; printf "%s -+- %s\n", map {strftime "%Y-%m-%d %H:%M:%S", localtime $_} $date, $end; } } process(@data); sub process { my @data = @_; # do somthing with data my @sum; my @idx = 2 .. 5; for my $line (@data) { for my $col (@idx) { $sum[$col] += $line->[$col]; } } printf "Avg of %d lines", scalar @data; print +(map {sprintf "%10.5f", $sum[$_] / @data} @idx), "\n"; } __DATA__ 2011.01.13,21:25,1.33508,1.33524,1.33470,1.33494,391 2011.01.13,21:30,1.33494,1.33506,1.33447,1.33453,318 2011.01.13,21:35,1.33453,1.33483,1.33417,1.33434,426 2011.01.13,21:40,1.33434,1.33468,1.33417,1.33467,309 2011.01.13,21:45,1.33471,1.33493,1.33465,1.33465,233 2011.01.13,21:50,1.33465,1.33475,1.33443,1.33463,184 2011.01.13,21:55,1.33463,1.33519,1.33463,1.33493,344 2011.01.13,22:00,1.33494,1.33563,1.33489,1.33524,318 2011.01.13,22:05,1.33524,1.33551,1.33512,1.33549,182 2011.01.14,22:05,1.33524,1.33551,1.33512,1.33549,182