Sha256: 1075cadd8495939ee25f22a8e0871e25cd44b58207760caaea700b095bbd6679

Contents?: true

Size: 1.69 KB

Versions: 36

Compression:

Stored size: 1.69 KB

Contents

#!/usr/bin/env ruby

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

options = SOPT.setup <<EOF
Read a TSV file and dump it

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

Use - to read from STDIN

-k--key_field* Key field
-f--fields* Fields
-t--type* Type
-s--sep* Separation character
-m--merge Merge from multiple rows
-h--help Print this help
-tch--tokyocabinet File is a tokyocabinet hash database
-tcb--tokyocabinet_bd File is a tokyocabinet B database

EOF
rbbt_usage and exit 0 if options[:help]

file = ARGV.shift

file = STDIN if file == '-'

case
when options[:tokyocabinet]
  tsv = Persist.open_tokyocabinet(file, false)
  puts tsv.to_s
  exit
when options[:tokyocabinet_bd]
  tsv = Persist.open_tokyocabinet(file, false, nil, TokyoCabinet::BDB)
  puts tsv.to_s
  exit
end

options[:fields] = options[:fields].split(/,\s*/) if options[:fields]

parser = TSV::Parser.new TSV.get_stream(file), options

dumper = TSV::Dumper.new parser.options.merge(:sep => "\t", :sep2 => '|')
dumper.init

options[:merge] = false if options[:merge] == "false"

Thread.new do
  line = parser.first_line
  bar = Log::ProgressBar.new
  while line 
    bar.tick

    line = Misc.fixutf8(line)
    line = parser.process line
    raise SKIP_LINE if line.empty?
    parts = parser.chop_line line
    key, values = parser.get_values parts
    values = parser.cast_values values if parser.cast?

    if Array === key
      key.each do |k|
        dumper.add k, values
      end
      line = parser.stream.gets
    else
      dumper.add key, values
      line = parser.stream.gets
    end
  end
  dumper.close
end

stream = dumper.stream
stream = TSV.collapse_stream(dumper.stream).stream if options[:merge]

while line = stream.gets
  puts line
end

Version data entries

36 entries across 36 versions & 1 rubygems

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