Split File Data into Multiple Files – Unix / Linux

  • Updated
  • Posted in Programming
  • 2 mins read


Q) I’ve file with 10000 strains in unix or linux working system. I wish to break up this file and create 10 information such that every file has 1000 strains. What I imply is the primary 100 strains ought to go into one file; subsequent 100 strains ought to go into one other file and so forth. How to do that utilizing unix instructions.

Solution:

Unix has the break up command which can be utilized to partition the information in a file into a number of information. The command to separate a file primarily based on the variety of strains is proven beneath:

break up -l 1000 filename

The above break up command splits the file such that every file has 1000 strains. Here the choice l signifies the variety of strains. You can break up the file primarily based on variety of bytes utilizing the -b choice.

break up -b 1024 filename

By default, the partitioned filenames begins with x like xab, xac, xad and so forth. Instead of alphabetical sequences, you need to use numeric sequences in filenames like x01, x02 utilizing the -d choice.

break up -l 1000 -d filename

You can specify the variety of digits for use within the numeric sequences with the assistance of -a choice.

break up -l 1000 -d -a 3 filename

Examples: Let say i’ve a textual content file with 4 strains. The knowledge within the file is proven beneath:

> cat textfile
unix is os
linux surroundings
centos
crimson hat linux

We will run the break up command for every of the factors mentioned above and see what information can be created.

> break up -l 2 textfile
Files: xaa, xab

> break up -b 10 textfile
Files: xaa, xab, xac, xad, xae

> break up -l 2 -d textfile
Files: x00, x01

> break up -l 2 -d -a 3 textfile
Files: x000, x001

Leave a Reply