What is a clever way to remove rows from a CSV file in ruby where a particular value exists in a particular row?
Here\'s an example of a file:
350 lbs.,
You can also create a Hash which would NOT allow duplicate records as its entries . For example, the following code should help:
require 'optparse'
require 'csv'
require 'pp'
options = Hash.new
OptionParser.new do |opts|
opts.banner = "Usage: remove_extras.rb [options] file1 ..."
options[:input_file] = ''
opts.on('-i', '--input_file FILENAME', 'File to have extra rows removed') do |file|
options[:input_file] = file
end
end.parse!
if File.exists?(options[:input_file])
p "Parsing: #{options[:input_file]}"
UniqFile=Hash.new
File.open(options[:input_file]).each do |row|
UniqFile.store(row,row.hash)
end
puts "please enter the output filename: \n"
aFile=File.open(gets.chomp, "a+")
UniqFile.each do|key,value|
aFile.syswrite("#{key}")
end
end