Skip to content

Instantly share code, notes, and snippets.

@rafael
Last active March 14, 2020 23:06
Show Gist options
  • Save rafael/f2996a3cbc45cab915c4be9f90e1bdc6 to your computer and use it in GitHub Desktop.
Save rafael/f2996a3cbc45cab915c4be9f90e1bdc6 to your computer and use it in GitHub Desktop.
#!/bin/ruby
require 'csv'
file_names = Dir["./*.csv"]
file_names.sort!
lines = []
file_names.each do |path|
i = 0
columns_in_header = 0
IO.readlines(path).each_with_index do |line, i|
columns_in_header = line.split(',').size if i == 0
line = line.gsub(/\n/, "")
line = line.gsub(/\r/, "")
date = path.gsub("./", "").gsub(".csv", "")
date = Date.strptime(date, "%m-%d-%y").strftime("%Y-%m-%d")
# Some files change the format after some time, so we need to add more padding.
line += ",," if columns_in_header == 6
# Appends the date of this entry to the csv
line += ",#{date}\n"
# do not append the header of the csv, we will do this at the end.
lines << line if i != 0
end
end
# creates an aggregated file
puts lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment