Sha256: 8c341a53d422b074ca3d30a6ea3483b232913f919fed9815f187840204329439

Contents?: true

Size: 1.59 KB

Versions: 217

Compression:

Stored size: 1.59 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
-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

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

217 entries across 217 versions & 1 rubygems

Version Path
rbbt-util-5.26.171 share/rbbt_commands/tsv/read
rbbt-util-5.26.170 share/rbbt_commands/tsv/read
rbbt-util-5.26.169 share/rbbt_commands/tsv/read
rbbt-util-5.26.168 share/rbbt_commands/tsv/read
rbbt-util-5.26.167 share/rbbt_commands/tsv/read
rbbt-util-5.26.166 share/rbbt_commands/tsv/read
rbbt-util-5.26.164 share/rbbt_commands/tsv/read
rbbt-util-5.26.163 share/rbbt_commands/tsv/read
rbbt-util-5.26.162 share/rbbt_commands/tsv/read
rbbt-util-5.26.161 share/rbbt_commands/tsv/read
rbbt-util-5.26.160 share/rbbt_commands/tsv/read
rbbt-util-5.26.159 share/rbbt_commands/tsv/read
rbbt-util-5.26.158 share/rbbt_commands/tsv/read
rbbt-util-5.26.157 share/rbbt_commands/tsv/read
rbbt-util-5.26.156 share/rbbt_commands/tsv/read
rbbt-util-5.26.155 share/rbbt_commands/tsv/read
rbbt-util-5.26.154 share/rbbt_commands/tsv/read
rbbt-util-5.26.153 share/rbbt_commands/tsv/read
rbbt-util-5.26.152 share/rbbt_commands/tsv/read
rbbt-util-5.26.151 share/rbbt_commands/tsv/read