Register
It is currently Sat Sep 20, 2014 12:00 pm

break up file by line, similar to `split`


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sat Aug 04, 2007 8:46 pm   

Joined: Sat Aug 04, 2007 8:18 pm
Posts: 2
Hi Everybody,

I have a text file that can have any number of lines.
I want to split the file into 6 files as evenly as possible (by lines)

I can do a simple

number=`cat file.txt.txt | wc -l`
number=`expr $number / 6 `
split -l $number file.txt

However this does not give a fair allocation of lines and does not always give 6 files.

Examples:
for a file with 22 lines I would like 4 files of 4 lines and 2 files of 3 lines, 4 4 4 4 3 3
for 23 lines I would like 5 files of 4 lines and 1 file of 3 lines, 4 4 4 4 4 3
for 24 lines I would like 6 files of 4 lines, 4 4 4 4 4 4
for 25 lines I would like 5 files of 4 lines and 1 of 5 lines, 4 4 4 4 4 5
for 26 lines I would like 4 files of 4 lines and 2 files of 5 lines, 4 4 4 4 5 5
for 27 lines I would like 3 files of 4 lines and 3 files of 5 lines, 4 4 4 5 5 5
and so on.

The numbers don't need to be in any particular order, for the last example it could be 5 5 5 4 4 4 or 4 5 5 4 4 5

Regards
Robert Stanford


Top
 Profile  
 PostPosted: Sun Aug 05, 2007 3:49 am   

Joined: Sat Aug 04, 2007 8:18 pm
Posts: 2
I worked it out.

Made algorithm using the remainder and a sequence.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP