43 lines
914 B
Ruby
43 lines
914 B
Ruby
|
|
require 'csv'
|
||
|
|
require 'optparse'
|
||
|
|
|
||
|
|
def process_csv(input_file)
|
||
|
|
# Open the input file and process it line by line
|
||
|
|
CSV.foreach(input_file) do |row|
|
||
|
|
# Write only the first two columns to stdout
|
||
|
|
puts row[0..1].to_csv
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def main
|
||
|
|
options = {}
|
||
|
|
|
||
|
|
# Define the command-line options
|
||
|
|
OptionParser.new do |opts|
|
||
|
|
opts.banner = "Usage: process_csv.rb -i INPUT_FILE"
|
||
|
|
|
||
|
|
opts.on("-i", "--input INPUT_FILE", "Path to the input CSV file") do |input|
|
||
|
|
options[:input] = input
|
||
|
|
end
|
||
|
|
end.parse!
|
||
|
|
|
||
|
|
# Check if the input file is provided
|
||
|
|
unless options[:input]
|
||
|
|
puts "Error: Input file is required."
|
||
|
|
puts "Usage: process_csv.rb -i INPUT_FILE"
|
||
|
|
exit 1
|
||
|
|
end
|
||
|
|
|
||
|
|
# Process the CSV file
|
||
|
|
begin
|
||
|
|
process_csv(options[:input])
|
||
|
|
rescue Errno::ENOENT
|
||
|
|
STDERR.puts "Error: File '#{options[:input]}' not found."
|
||
|
|
exit 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
# Run the script
|
||
|
|
main if __FILE__ == $PROGRAM_NAME
|
||
|
|
|