#!/usr/bin/env ruby #!/usr/local/bin/ruby # Sort a table given a column name. # usage ruby -s rsort.rb [-column=one] [-header=2] [filenames] # The input must contain a header line. # This header contains column titles. # All lines before the header are ignored. # Eric Blossom - September 2004 header = 1 header = $header.to_i() if $header != nil theColumn = "" theColumn = $column if $column != nil $; = "\t" rows = Array.new while gets line = chomp.split() next if $. < header if header == $. colNames = line if theColumn sortKey = line.index( theColumn ) end sortKey = 0 if ! sortKey next end rows.insert( -1, line ) end rows.sort! { |a, b| a[sortKey] <=> b[sortKey] } puts colNames.join( $; ) rows.each { |r| puts r.join( $; ) }