Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 38 lines (30 sloc) 754 Bytes
#!/usr/bin/env ruby
$doc = %{
yaml2tsv
Takes a YAML document stream -- that is, a concatenation of YAML top-level
objects -- and treats them as key/value records to be turned into
tsv-with-header.
}
require 'yaml'
require 'pp'
if STDIN.tty?
STDERR.puts $doc.strip
exit 1
end
all = []
YAML.load_documents(STDIN) do |ydoc|
all << ydoc
end
cols = all.map{|x| x.keys}.flatten.uniq.sort
puts cols.map{|c| c.gsub(/\s/," ")}.join("\t")
all.each do |ydoc|
puts cols.map{|c| (ydoc[c] || "").to_s.gsub(/\s/," ")}.join("\t")
end
# cols = nil
# YAML.load_documents(STDIN) do |ydoc|
# if cols.nil?
# cols = ydoc.keys.sort
# puts cols.map{|c| c.gsub(/\s/," ")}.join("\t")
# end
# puts cols.map{|c| ydoc[c].gsub(/\s/," ")}.join("\t")
# end