Sha256: e38b048b09f9bf1350721b1d53265708c4cd67f057fd3e8053f2bf6a88d2932c

Contents?: true

Size: 1.01 KB

Versions: 22

Compression:

Stored size: 1.01 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)
-h--help Help
EOF

SOPT.usage if options[:help]

file = ARGV.shift

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

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.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)

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rbbt-util-5.19.17 share/rbbt_commands/tsv/sort
rbbt-util-5.19.16 share/rbbt_commands/tsv/sort
rbbt-util-5.19.15 share/rbbt_commands/tsv/sort
rbbt-util-5.19.14 share/rbbt_commands/tsv/sort
rbbt-util-5.19.13 share/rbbt_commands/tsv/sort
rbbt-util-5.19.12 share/rbbt_commands/tsv/sort
rbbt-util-5.19.11 share/rbbt_commands/tsv/sort
rbbt-util-5.19.10 share/rbbt_commands/tsv/sort
rbbt-util-5.19.9 share/rbbt_commands/tsv/sort
rbbt-util-5.19.8 share/rbbt_commands/tsv/sort
rbbt-util-5.19.7 share/rbbt_commands/tsv/sort
rbbt-util-5.19.6 share/rbbt_commands/tsv/sort
rbbt-util-5.19.5 share/rbbt_commands/tsv/sort
rbbt-util-5.19.4 share/rbbt_commands/tsv/sort
rbbt-util-5.19.3 share/rbbt_commands/tsv/sort
rbbt-util-5.19.2 share/rbbt_commands/tsv/sort
rbbt-util-5.19.1 share/rbbt_commands/tsv/sort
rbbt-util-5.19.0 share/rbbt_commands/tsv/sort
rbbt-util-5.18.1 share/rbbt_commands/tsv/sort
rbbt-util-5.18.0 share/rbbt_commands/tsv/sort