Created
September 27, 2012 06:53
-
-
Save nanki/3792570 to your computer and use it in GitHub Desktop.
CUI pivot table.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: UTF-8 -*-; | |
gem 'text_layout' | |
require 'optparse' | |
require 'text_layout' | |
require 'set' | |
opts = OptionParser.new <<EOS | |
Usage: #{$0} [options] | |
pivot table. | |
EOS | |
c = {} | |
opts.on("-h", "--header", "Header") {|v| c[:header] = v } | |
opts.on("-n", "--numeric-sort", "Sort by numerical value.") {|v| c[:numeric] = true } | |
opts.on("-1", "--field1=", "Field 1") {|v| c[:f1] = v.to_i } | |
opts.on("-2", "--field2=", "Field 2") {|v| c[:f2] = v.to_i } | |
opts.on("-m", "--mark=", "mark") {|v| c[:mark] = v } | |
opts.parse!(ARGV) | |
c[:header] = true if c[:header].nil? | |
acc = {} | |
cols = Set.new | |
Header = Struct.new(:column, :row) | |
header = nil | |
f1 = c[:f1] || 0 | |
f2 = c[:f2] || 1 | |
ARGF.each do |line| | |
line.strip! | |
vs = line.split | |
if !header && c[:header] | |
header = Header.new(vs[f1], vs[f2]) | |
next | |
end | |
acc[vs[f1]] = Hash.new{0} unless acc[vs[f1]] | |
acc[vs[f1]][vs[f2]] += 1 | |
cols << vs[f2] | |
end | |
key = c[:numeric] ? :to_i : :to_s | |
cols = cols.sort_by(&key) | |
result = acc.keys.sort_by(&key).map do |row| | |
line = cols.map do |col| | |
v = (acc[row] || {})[col] | |
v == 0 ? nil : (c[:mark] || v) | |
end | |
line.unshift row | |
end | |
result.unshift [nil] + cols | |
puts TextLayout::Table.new(result).layout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment