Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to split files into guaranteed number of buckets. The size do not have to be equal.

Here is my logic

count = wc -l filename
split_count = (count)/4
split -l split_count filename core_

Sometimes if I change the numerate of 4 to another number it gives me N+1 number of buckets. Is there a better way to to create exactly N buckets?

share|improve this question

migrated from Oct 8 '11 at 23:38

This question came from our site for professional and enthusiast programmers.

Why was this migrated from SO? This is clearly a programming question because (1) it has a shell tag, indicating shell scripting; and (2) there is code to calculate the split count. – paxdiablo Oct 8 '11 at 23:43
up vote 2 down vote accepted

It's probably because the result of using integer arithmetic will sometimes cause a value slightly too small.

For example, 1001 / 4 is 250 but four lots of 250 lines will not do the entire 1001 lines you have.

You can modify your logic to increase that value until four of them are at least as big as the file, something like:

count = wc -l filename
split_count = (count)/4
while split_count * 4 < count:           # Add these
    split_count++                        #   two lines.
split -l split_count filename core_

You could also round up to the next multiple of N to ensure it's enough, something like:

split_count = (count + N - 1) / N
share|improve this answer
worked like a charm:) Thanks – Monty Oct 2 '11 at 0:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.