Sha256: c9b08014d1818ea9326516d2df8a55ebb9048fddd711e8dd8eceb0c13514cd0d

Contents?: true

Size: 1.4 KB

Versions: 46

Compression:

Stored size: 1.4 KB

Contents

#!/usr/bin/env ruby

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

options = SOPT.setup <<EOF

Query a TSV value

$ rbbt tsv get [options] <filename.tsv|-> <key>

Use - to read from STDIN

-tch--tokyocabinet File is a tokyocabinet hash database
-tcb--tokyocabinet_bd File is a tokyocabinet B database
-f--fields* Fields to extract
-k--key_field* Use this field as key
-h--help Print this help
-l--lines Separate in lines
EOF

rbbt_usage and exit 0 if options[:help]

file, key = ARGV

raise ParameterException, "Please specify file and key" if key.nil?

file = STDIN if file == '-'

case
when options[:tokyocabinet]
  tsv = Persist.open_tokyocabinet(file, false)
when options[:tokyocabinet_bd]
  tsv = Persist.open_tokyocabinet(file, false, nil, TokyoCabinet::BDB)
else
  if String === file
    file = file.dup
    Path.setup(File.expand_path(file))
  end
  tsv = file
end

fields = options[:fields]
key_field = options[:key_field]
fields = fields.split(/[,|]/, -1) unless fields.nil?

parser = TSV::Parser.new tsv, :key_field => key_field,  :fields => fields
fields ||= parser.fields

TSV.traverse(parser) do |k,v|
  next unless k.include? key
  if fields.length == 1
    if options[:lines]
      puts (Array === v ? v.flatten*"\n" : v.to_s ) 
    else
      puts (Array === v ? v.flatten*"\t" : v.to_s ) 
    end
  else
    fields.zip(v).each do |field,v|
      puts "#{Log.color :magenta, field+":"} #{v}"
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
rbbt-util-5.22.4 share/rbbt_commands/tsv/get
rbbt-util-5.22.3 share/rbbt_commands/tsv/get
rbbt-util-5.22.1 share/rbbt_commands/tsv/get
rbbt-util-5.22.0 share/rbbt_commands/tsv/get
rbbt-util-5.21.137 share/rbbt_commands/tsv/get
rbbt-util-5.21.136 share/rbbt_commands/tsv/get
rbbt-util-5.21.135 share/rbbt_commands/tsv/get
rbbt-util-5.21.134 share/rbbt_commands/tsv/get
rbbt-util-5.21.133 share/rbbt_commands/tsv/get
rbbt-util-5.21.132 share/rbbt_commands/tsv/get
rbbt-util-5.21.131 share/rbbt_commands/tsv/get
rbbt-util-5.21.130 share/rbbt_commands/tsv/get
rbbt-util-5.21.129 share/rbbt_commands/tsv/get
rbbt-util-5.21.128 share/rbbt_commands/tsv/get
rbbt-util-5.21.127 share/rbbt_commands/tsv/get
rbbt-util-5.21.126 share/rbbt_commands/tsv/get
rbbt-util-5.21.125 share/rbbt_commands/tsv/get
rbbt-util-5.21.123 share/rbbt_commands/tsv/get
rbbt-util-5.21.122 share/rbbt_commands/tsv/get
rbbt-util-5.21.121 share/rbbt_commands/tsv/get