Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com Oct 8 '11 at 23:38

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

1  
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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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