Skip to content

Instantly share code, notes, and snippets.

@hyoshiok
Created October 13, 2011 05:05
Show Gist options
  • Save hyoshiok/1283436 to your computer and use it in GitHub Desktop.
Save hyoshiok/1283436 to your computer and use it in GitHub Desktop.
cut a field of csv file.
# encodings: utf-8
=begin :rdoc
= cut_f.rb
This script prints a field of CSV file.
The format of the csv file is the following
id, replied_to_id, thread_id, timestamp, group, recipients, private, user_id, user, email_address, body, url, attachment_ids
= usage
ruby cut_f.rb field-number <csv-file>
= output
print n-th field
Author :: hirotaka.yoshioka@mail.rakuten.com
Date :: 6/2/2011 created
= history
06/02/2011 initial
=end
require 'csv'
field_num = ARGV[0].to_i
if ARGV[1] == "-" or ARGV[1] == nil
io = $stdin
else
io = File.open(ARGV[1], "r")
end
CSV::Reader.create(io.read).each do |row|
print row[field_num], "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment