#!/usr/bin/ruby require 'gdbm' cols = %w(gts d12 la1 lo1 la2 lo2 ia2 sn1 cn1 hp1 ha1 ra1 sn2) sortp = nil while /^-/ === ARGV.first case ARGV.shift when /^-s/ then sortp = true end end dbfile = ARGV.shift raise "usage: #$0 [-s] dbfile [input ...]" unless dbfile puts (['#cccc'] + cols).join("\t") GDBM.open(dbfile) {|db| keys = db.keys if sortp STDERR.puts "sorting" keys.sort!{|a,b| ra = Hash[*db[a].to_s.split(/\t/).map{|s| s.split(/:/, 2)}.flatten] rb = Hash[*db[b].to_s.split(/\t/).map{|s| s.split(/:/, 2)}.flatten] rb['d12'].to_s.to_f <=> ra['d12'].to_s.to_f } end for cccc in keys r = Hash[*db[cccc].to_s.split(/\t/).map{|s| s.split(/:/, 2)}.flatten] puts ([cccc] + r.values_at(*cols)).join("\t") end }