Skip to content

Instantly share code, notes, and snippets.

@remore
Last active December 18, 2015 17:59
Show Gist options
  • Save remore/5822886 to your computer and use it in GitHub Desktop.
Save remore/5822886 to your computer and use it in GitHub Desktop.
# usage: cat accumulated_number_data.ltsv | ruby calc_delta.rb
#!/usr/bin/env ruby
# usage: cat accumulated_number_data.ltsv | ruby calc_delta.rb
class LtsvDataLine
attr_accessor :data
def initialize(line)
@data = Hash.new
line.split("\t").each{|packet|
@data[packet.split(":")[0]] = packet.split(":")[1]
}
end
def to_s
s = ""
@data.sort.each{|key, value|
s = s + "#{key}:#{value}\t"
}
s
end
end
previous_line = nil
while gets
if previous_line.nil? then
# do nothing
else
current_line = LtsvDataLine.new($_.chomp)
current_line.data.each{|key, value|
if key!="date" then
if previous_line.data[key].nil? then
current_line.data.delete(key)
else
current_line.data[key] = value.to_i - previous_line.data[key].to_i
end
end
}
puts sprintf("date:%s\t%s", current_line.data.delete("date"), current_line.to_s )
end
previous_line = LtsvDataLine.new($_.chomp)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment