Skip to content

Instantly share code, notes, and snippets.

@stanaka
Created February 16, 2013 15:36
Show Gist options
  • Save stanaka/4967403 to your computer and use it in GitHub Desktop.
Save stanaka/4967403 to your computer and use it in GitHub Desktop.
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