Created
April 20, 2009 08:52
-
-
Save arunthampi/98438 to your computer and use it in GitHub Desktop.
Split File into multiple pieces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
input_file = ARGV[0] | |
# This function is stolen from: | |
# http://snippets.dzone.com/posts/show/3486 | |
def chunk_array(array, pieces=2) | |
len = array.length; | |
mid = (len/pieces) | |
chunks = [] | |
start = 0 | |
1.upto(pieces) do |i| | |
last = start+mid | |
last = last-1 unless len%pieces >= i | |
chunks << array[start..last] || [] | |
start = last+1 | |
end | |
chunks | |
end | |
contents = File.read(input_file).split("\n") | |
content_chunks = chunk_array(contents, 1000) | |
content_chunks.each_with_index do |chunk, idx| | |
File.open("#{File.basename(input_file)}.#{idx+1}", "w") do |out| | |
out.puts(chunk.join("\n")) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment