Linux csplit Command
The Linux csplit command is used to split files.
It splits the file according to specified pattern(s) and saves them as files named xx00, xx01, xx02, and so on. If the given filename is "-", the csplit command reads data from the standard input device.
Syntax
csplit [-kqsz][-b<output format>][-f<output prefix string>]
[-n<output file name digits>][--help][--version][file][pattern...]
Parameters:
-b<output format> or --suffix-format=<output format> The default output format has filenames like xx00, xx01, etc. You can change the output filenames by modifying the <output format>.
-f<output prefix string> or --prefix=<output prefix string> The default output prefix string has filenames like xx00, xx01, etc. If you specify the output prefix string as "hello", the output filenames will become hello00, hello01, etc.
-k or --keep-files Retains the files, even if an error occurs or the execution is interrupted, preventing the deletion of already saved output files.
-n<output file name digits> or --digits=<output file name digits> The default output file name digits have filenames like xx00, xx01, etc. If you specify the output file name digits as "3", the output filenames will become xx000, xx001, etc.
-q or -s or --quiet or --silent Does not display the command execution process.
-z or --elide-empty-files Deletes files with a length of 0 bytes.
--help Online help.
--version Displays version information.
Example
To split the text file testfile into two parts at the second line, use the following command:
csplit testfile 2
The content of the testfile is as follows:
$ cat testfile # View the content of testfile
hello Linux!
Linux is a free Unix-type operating system.
This is a Linux testfile!
Linux
Using the csplit command, the output is as follows:
$ csplit testfile 2
13 # Character count of xx00 file
76 # Character count of xx01 file
The first line is the character count of the first file xx00, and the second line is the character count of the second file xx01. Additionally, two files named xx00 and xx01 will be created in the same directory as testfile. The content of xx00 is:
$ cat xx00 # View the content of the split xx00 file
hello Linux! # Content of the first line of testfile
The content of xx01 is:
$ cat xx01 # View the content of the split xx01 file
Linux is a free Unix-type operating system. # Content from the second line onwards of testfile
This is a Linux testfile!
Linux