misc_rbtools/command_line/dump_csv.rb

30 lines
711 B
Ruby
Raw Normal View History

2018-03-23 15:50:37 -05:00
#!/usr/bin/env ruby
# dump csv file contents
require 'csv'
require 'optparse'
# basic commandline parsing
options = {}
parser = OptionParser.new do |parser|
parser.banner = "Usage: dump_csv.rb [options]"
parser.on("-c", "--column int", "The column number to dump.") do |column|
2018-03-23 15:50:37 -05:00
options[:column] = column
end
parser.on("-f", "--filename FILE", "The CSV filename to use.") do |filename|
2018-03-23 15:50:37 -05:00
options[:filename] = filename
end
options[:all] = false
parser.on("-a", "--all", "Dump all columns") do
options[:all] = true
end
2018-03-23 15:50:37 -05:00
end
parser.parse!
col_data = []
CSV.foreach (options[:filename]) {|col| col_data << col[options[:column].to_i]} if (options[:column] and options[:filename])
2018-03-23 15:50:37 -05:00
puts col_data