Sha256: 222cd1a1526e52e605ff4a7b426e3442f1a43540b1c3f3866d1b3f6d061d69bb

Contents?: true

Size: 1.42 KB

Versions: 589

Compression:

Stored size: 1.42 KB

Contents

#!/usr/bin/env ruby

require 'rbbt-util'
require 'rbbt/util/simpleopt'

$0 = "rbbt #{$previous_commands*""} #{ File.basename(__FILE__) }" if $previous_commands

options = SOPT.setup <<EOF
Sort a TSV file, mantaining headers

$ rbbt tsv sort file.tsv

Display summary information. Works with Tokyocabinet HDB and BDB as well.

-hh--header_hash* Change the character used to mark the header line (defaults to #)
-f--field* Field to sort by (name or number)
-a--absolute Sort as absolute values
-h--help Help
EOF

SOPT.usage if options[:help]

file = ARGV.shift

file = STDIN if file == '-' or file.nil?

if options[:absolute]
  tsv = TSV.open(file, options)
  dumper = TSV::Dumper.new tsv.options
  dumper.init
  field = options[:field] || 1
  keys = tsv.sort(field) do |a| a = a.first if Array === a; a.to_f.abs end
  TSV.traverse keys, :type => :array, :into => dumper do |key|
    values = tsv[key]
    [key,values]
  end
  stream = dumper.stream
else
  field = options[:field] || '2'

  if field =~ /^\d+$/
    field_pos = field.to_i
  else
    parser = TSV.parse_header(file, options)
    field_pos = parser.all_fields.index(field) + 1
    saved_line = parser.first_line
  end

  rest = ARGV.inject("-t\"\t\" -g -k#{field_pos}"){|acc,e| acc << " '#{e}'" }

  file_io = TSV.get_stream(file)
  stream = Misc.sort_stream(file_io, options[:header_hash] || "#", rest)
end

begin
  while line = stream.gets
    puts line
  end
rescue Exception
  exit -1
end

Version data entries

589 entries across 589 versions & 1 rubygems

Version Path
rbbt-util-5.44.1 share/rbbt_commands/tsv/sort
rbbt-util-5.43.0 share/rbbt_commands/tsv/sort
rbbt-util-5.42.0 share/rbbt_commands/tsv/sort
rbbt-util-5.41.1 share/rbbt_commands/tsv/sort
rbbt-util-5.41.0 share/rbbt_commands/tsv/sort
rbbt-util-5.40.5 share/rbbt_commands/tsv/sort
rbbt-util-5.40.4 share/rbbt_commands/tsv/sort
rbbt-util-5.40.3 share/rbbt_commands/tsv/sort
rbbt-util-5.40.0 share/rbbt_commands/tsv/sort
rbbt-util-5.39.0 share/rbbt_commands/tsv/sort
rbbt-util-5.38.1 share/rbbt_commands/tsv/sort
rbbt-util-5.38.0 share/rbbt_commands/tsv/sort
rbbt-util-5.37.16 share/rbbt_commands/tsv/sort
rbbt-util-5.37.15 share/rbbt_commands/tsv/sort
rbbt-util-5.37.14 share/rbbt_commands/tsv/sort
rbbt-util-5.37.13 share/rbbt_commands/tsv/sort
rbbt-util-5.37.12 share/rbbt_commands/tsv/sort
rbbt-util-5.37.11 share/rbbt_commands/tsv/sort
rbbt-util-5.37.10 share/rbbt_commands/tsv/sort
rbbt-util-5.37.9 share/rbbt_commands/tsv/sort