Skip to content

Instantly share code, notes, and snippets.

@parkr
Created December 30, 2011 00:17
Show Gist options
  • Save parkr/1536867 to your computer and use it in GitHub Desktop.
Save parkr/1536867 to your computer and use it in GitHub Desktop.
Amazon Order Reports: Count the Number of Purchases Per Day Given a Keyword
#!/usr/bin/env ruby -wKU
#
# This script produces a CSV file of day-by-day sales given a keyword.
#
keyword = /Concert Ticket */
unless ARGV.empty?
filename = ARGV[0].to_s.strip
else
raise ArgumentError, "You must enter a filename."
end
sales = {}
counter = 0
IO.foreach("Sales Reports/#{filename}") do |line|
unless counter == 0
cols = line.gsub(/(\r?\n)|\r/, "\n").split("\t")
if cols[8].match(keyword) != nil
#puts "Purchase Date: #{cols[2]}"
#puts "Product Name: #{cols[8]}"
#puts "Num Purchased: #{cols[9]}"
if sales[cols[2].to_s[0..9]]
sales[cols[2].to_s[0..9]] << (cols[9].to_i)
else
sales[cols[2].to_s[0..9]] = (cols[9].to_i)
end
end
end
counter += 1
end
output = "Purchase Date,Num Purchased\n"
sales.each do |date, num|
output << "#{date},#{num}\n"
end
newfilename = "#{filename.gsub(File.extname(filename), '')}.csv"
File.open(newfilename, "w") do |f|
f.write(output)
end
puts "Written to ./#{newfilename}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment