Skip to content

Instantly share code, notes, and snippets.

@remore
Last active December 17, 2015 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save remore/5530577 to your computer and use it in GitHub Desktop.
Save remore/5530577 to your computer and use it in GitHub Desktop.
usage: ruby ltsv2csv.rb --file hoge.ltsv
#!/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|
if labels.index(data.split(":")[0]) == nil then
labels << data.split(":")[0]
end
row[labels.index(data.split(":")[0])] = data.split(":")[1]
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