Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a file into n no of parts

Tags:

file

split

I have a file contining some no of lines. I want split file into n no.of files with particular names. It doesn't matter how many line present in each file. I just want particular no.of files (say 5). here the problem is the no of lines in the original file keep on changing. So I need to calculate no of lines then just split the files into 5 parts. If possible we have to send each of them into different directories.

like image 246
new person Avatar asked Jul 07 '10 11:07

new person


People also ask

How do I split a file into parts in Windows?

Right-click the file and select the Split operation from the program's context menu. This opens a new configuration window where you need to specify the destination for the split files and the maximum size of each volume. You can select one of the pre-configured values or enter your own into the form directly.

How do you split a file into equal parts in Unix?

To split a file equally into two files, we use the '-n' option. By specifying '-n 2' the file is split equally into two files.


Video Answer


2 Answers

In bash, you can use the split command to split it based on number of lines desired. You can use wc command to figure out how many lines are desired. Here's wc combined with with split into one line.

For example, to split onepiece.log into 5 parts

    split -l$((`wc -l < onepiece.log`/5)) onepiece.log onepiece.split.log -da 4 

This will create files like onepiece.split.log0000 ...

Note: bash division rounds down, so if there is a remainder there will be a 6th part file.

like image 154
sketchytechky Avatar answered Oct 06 '22 10:10

sketchytechky


On linux, there is a split command,

split --lines=1m /path/to/large/file /path/to/output/file/prefix 

Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default size is 1000 lines, and default PREFIX is 'x'. With no INPUT, or when INPUT is -, read standard input.

...

-l, --lines=NUMBER put NUMBER lines per output file

...

You would have to calculate the actual size of the splits beforehand, though.

like image 41
miku Avatar answered Oct 06 '22 12:10

miku