-
-
Save rch850/8725447 to your computer and use it in GitHub Desktop.
This file contains 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/ruby | |
require 'optparse' | |
separator = "," | |
source = false | |
ARGV.clone.options do |opts| | |
opts.banner = "Usage: #{$0} [options]" | |
opts.on('-f', '--file=/path/to/file', String, 'Source file'){|v| | |
source = v | |
} | |
opts.on('-t', '--tab', 'Use tab for separator'){ | |
separator = "\t" | |
} | |
opts.on('-h', '--help', 'Show this help message.'){ puts opts; exit } | |
opts.order! { |o| file ||= o } rescue retry | |
end | |
io = source ? open(source, 'r') : STDIN | |
labels = Array.new(); | |
rows = Array.new(); | |
while ltsv = io.gets | |
row = Array.new(); | |
ltsv.chomp.split("\t").each do |data| | |
label, value = data.split(":", 2) | |
if labels.index(label) == nil then | |
labels << label | |
end | |
row[labels.index(label)] = value | |
end | |
rows << row.join(separator) | |
end | |
puts labels.join(separator) | |
puts rows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment