Skip to content

Instantly share code, notes, and snippets.

@stanaka stanaka/ltsv2json.rb
Created Feb 16, 2013

Embed
What would you like to do?
a converter from LTSV to JSON.
#!/usr/bin/env ruby
require 'optparse'
require 'json'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ltsv2json.rb [options]"
opts.on("-f", "--format FORMAT", "Select format.") do |v|
options[:format] = v
end
end.parse!
while gets
record = Hash[$_.split("\t").map{|f| f.split(":", 2)}]
case options[:format]
when 'ltsv'
puts $_
when 'ssv'
puts record.values.join(' ')
else
puts record.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.