use Modern::Perl; use Date::Calc qw/Date_to_Time/; my $duration = 10 * 60; my $fileName = 'data.txt'; open my $fh, '<', $fileName or die "Unable to open file: $!"; for ( ; ; ) { my ( $first, @lines ); while ( my $line = <$fh> ) { my ( $date, $time, $values ) = split ',', $line, 3; my @dateTime = "$date $time 0" =~ /(\d+)/g; my $timeInSecs = Date_to_Time @dateTime; $first = $timeInSecs unless $first; push @lines, "$timeInSecs\t$values"; last if $timeInSecs - $first >= $duration; } # Start work with chunk of lines in array # do { /(\S+)\t(\S+)/; say "Seconds: $1; Values: $2" } for @lines; say '----------'; # # End work with chunk of lines in array last if eof $fh; } close $fh;