Last active
December 17, 2015 01:48
-
-
Save remore/5530577 to your computer and use it in GitHub Desktop.
usage: ruby ltsv2csv.rb --file hoge.ltsv
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/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