Split a file into 16 small files

Hi

I want to split a file that has 'n' number of records into 16 small files.

Can some one suggest me how to do this using Unix script?

Thanks
rrkk

Have a look at "split" man page:

man split

and then, assuming n=100, 100/16=6.25 (rounded to 7):

split -7 input_file.txt

you could use the split command. Have a look at the -l (lines) and -b (bytes) parameters depending on whether your records are line delimited or size delimited.

Hi

my file is a line delimited and am new to unix, can you give me the script how to do it.

keep in mind that every time the number of records may change but it has to be split only into 16 small file.

Thanks
rrkk

therefore the steps to take are:

1, count the number of records in the file (i.e. number of lines) - the wc utility can do this

  1. divide the total number of records by 16 to get a line count for each sub file

  2. use split -l <number of lines> to split the file into 16 sub files

  3. optionally delete the original file

let us know if you have trouble writing these steps into a script, posting the script that you write, and we'll help further.

Can you pls give me the Unix code as am a newb to it.

I appreciate your help.

Thanks
rrkk

I am loathe to just write the script for you, if I do you won't learn anything.

Bash Beginners Guide at tldp.org is a good starting point if you have no idea where to start.

the advanced guide is also alvailable at the same site for when you feel a little more confident.

Have a go at writing a script based on the steps I outlined above and when you get into trouble come back here and ask.

I really appreciate your help and thank you once again for your suggestion.

I want to split a file which has 'n' number of records into 16 small files.
does this code help me.

rm ZCSCRA_SPLIT_NO_*
wc -l ZCSCRA_NO.CSV
expr -l/16 b

split -l <b> ZCSCRA_NO.CSV ZCSCRA_SPLIT_NO_

very nearly

try

numlines=`wc -l ZCSCRA_NO.CSV | cut -d" " -f1`
splitlines=`expr $numlines / 16`
modulus=`expr $numlines % 16`
if [ $modulus -gt 0 ]
then
splitlines=$((splitlines+1))
fi
split -l $splitlines ZCSCRA_NO.CSV ZCSCRA_SPLIT_NO_

note:

  1. because wc will return not only the number of lines in the file, but also the name of the file you will need to use another utility like sed or cut, to chop up the returned string and just return the number. In this case I used cut with a delimiter of a space (-d" ") and asked it to return the first field (-f1)

  2. if your shell is bash/ksh then it can do basic arithmetic without using a call to expr.

  3. I've put a check in to see if there was a remainder from the division as both expr and bash arithmetic only return integers.

Hope this helps.

I really really appreciate your help...You are inspirable to many people.

hatsoff to you..

Thanks
rrkk