#!/usr/bin/env ruby require 'time' if(ARGV.length < 2) puts "Usage: timesplit " exit 1 end timeunit = ARGV.shift command = ARGV.join ' ' case timeunit when /seconds?/ $seconds = 1 when /minutes?/ $seconds = 60 when /hours?/ $seconds = 60 * 60 else puts "Unrecognized timeunit #{timeunit}" $seconds = 1 end if timeunit =~ /(\d+)/ $seconds = $seconds * $1.to_i end $last_timestamp = Time.at(0) class Time def floor(seconds = 60) Time.at((self.to_f / seconds).floor * seconds) end end def get_timestamp(timeunit, line) if line =~ /\<(\d\d\:\d\d\:\d\d)>/ time = Time.parse($1) time.floor($seconds) elsif line =~ /\<(\d\d ... \d\d\d\d \d\d\:\d\d\:\d\d\,\d\d\d)>/ time = Time.parse($1) time.floor($seconds) else $last_timestamp end end $lastpid = nil def launch_child(command, timestamp = nil) Process.waitpid($lastpid) if $lastpid r, w = IO.pipe puts timestamp.to_s if timestamp $lastpid = fork do w.close $stdin.reopen(r) exec command end r.close w end w = launch_child(command) $stdin.each_line do |line| timestamp = get_timestamp(timeunit, line) if(timestamp > $last_timestamp) w.close w = launch_child(command, timestamp) $last_timestamp = timestamp end w.puts line end w.close