csplit Command Examples in Linux

The csplit command is a utility in Linux that is used to split a file into multiple smaller files based on specified criteria. It can split a file based on the occurrence of a particular string or pattern, or at fixed intervals of lines or bytes.

Here is an example of using the csplit command to split a file into multiple smaller files based on the occurrence of a particular string:

# csplit INPUT_FILE '/PATTERN/' '{*}'

This will split the INPUT_FILE into multiple smaller files, with each file containing all of the lines up to (but not including) the next occurrence of the PATTERN string. For example, if you have a file named “input.txt” that contains the following lines:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

And you want to split the file into multiple smaller files based on the occurrence of the string “Line 5”, you can use the following command:

# csplit input.txt '/Line 5/' '{*}'

This will create two new files: “xx00” and “xx01”, with the following contents:

xx00:

Line 1
Line 2
Line 3
Line 4

xx01:

Line 6
Line 7
Line 8
Line 9
Line 10

csplit Command Examples

1. Split a file at lines 5 and 23:

# csplit file 5 23

2. Split a file every 5 lines (this will fail if the total number of lines is not divisible by 5):

# csplit file 5 {*}

3. Split a file every 5 lines, ignoring exact-division error:

# csplit -k file 5 {*}

4. Split a file at line 5 and use a custom prefix for the output files:

# csplit file 5 -f prefix

5. Split a file at a line matching a regular expression:

# csplit file /regular_expression/
Related Post