Skip to content

Instantly share code, notes, and snippets.

@bartlomiejdanek
Created September 23, 2013 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartlomiejdanek/6668216 to your computer and use it in GitHub Desktop.
Save bartlomiejdanek/6668216 to your computer and use it in GitHub Desktop.
split your csv files
require 'csv'
infile = ARGV[0]
slice = ARGV[1] || 100
origin = CSV.open infile
all_lines = origin.readlines
header = all_lines.first
all_lines.delete_at(0)
all_lines.each_slice(slice).each_with_index do |lines, idx|
CSV.open("splitted_#{idx}.csv", 'w') do |csv|
csv << header
lines.each do |line|
csv << line
end
end
end
# $ ruby splitter.rb file.csv 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment