Skip to content

Instantly share code, notes, and snippets.

@rch850
Forked from remore/ltsv2csv.rb
Last active August 29, 2015 13:55
Show Gist options
  • Save rch850/8725447 to your computer and use it in GitHub Desktop.
Save rch850/8725447 to your computer and use it in GitHub Desktop.
#!/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