5+ Practical “csplit” Command Usage Examples in Linux

«
»

This tutorial explains Linux “csplit” command, options and its usage with examples.

“csplit” command is used to split a file into sections determined by context lines. This post describes “csplit” command used in Linux along with usage examples and/or output.

Usage:
csplit [OPTION]… FILE PATTERN…
The patterns may be line numbers or regular expressions. The program outputs pieces of the file separated by the patterns into files xx00, xx01, etc., and outputs the size of each piece, in bytes, to standard output.
Exit Status:
* 0 Successful completion.
* >0 An error occurred.

advertisement

The csplit command writes the segments to files xx00 . . . xx99, depending on how many times the Argument parameter is specified (99 is the maximum).So for example if file 1.txt has 123 lines ans you entered:

csplit 1.txt 13 62 101

the csplit command would create four files: the xx00 file would contain lines 1-12, the xx01 file would contain lines 13-61, the xx02 file would contain lines 62-100, the xx03 file would contain lines 101-123.

Here’s the listing of example usage of csplit command:

advertisement
advertisement

Let a file sample.txt contains:

sanfoundry-> cat sample.txt 
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

1. Make a file to break at a particular line number(csplit file_name number):

sanfoundry-> csplit sample.txt 5
28
43
sanfoundry-> ls
sample.txt  xx00  xx01
sanfoundry-> cat xx00
Line 1
Line 2
Line 3
Line 4
sanfoundry-> cat xx01 
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

The output of the command is number of the bytes in which the files are broken. After executing the command two new files xx00 and xx01 are created of 28 and 43 bytes respectively.

advertisement

2. To split files at a regular expression(csplit file_name reg_expression) :

sanfoundry-> csplit sample.txt /4/
21
50
sanfoundry-> cat xx00 
Line 1
Line 2
Line 3
sanfoundry-> cat xx01 
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

Here as you can see that the file is break at a point where a regular expression string”4″ has appeared in the file and two different files,xx00 and xx01 are created. Also if expression is found at starting a empty file xx00 is created.

sanfoundry-> csplit sample.txt /Line/
0
71

3. To split a file in a Repeating pattern(csplit file_name pattern):

advertisement
sanfoundry-> csplit sample.txt 1 {7}
0
7
7
7
7
7
7
7
22
sanfoundry-> ls
sample.txt  xx00  xx01	xx02  xx03  xx04  xx05	xx06  xx07  xx08
sanfoundry-> cat xx00 
sanfoundry-> cat xx02
Line 2
sanfoundry-> cat xx07
Line 7
sanfoundry-> cat xx08
Line 8
Line 9
Line 10

In the above command we passed “1” as the pattern and 7 as integer for repeat. Thus the file was split 7 times and having 1 line in each file and after 7 times all the remaining contents are filled in the file xx08. But, if number of lines are enough to break files in given number of lines, we get error message as:

sanfoundry-> csplit sample.txt 2 {7}
7
14
14
14
14
csplit: `2': line number out of range on repetition 5
8

4. To set output file prefix as desired(csplit -f desired_name file_name …. pattern):

advertisement
sanfoundry-> csplit -f xyz sample.txt 6
35
36
sanfoundry-> ls
sample.txt  xyz00  xyz01
sanfoundry-> cat xyz00
Line 1
Line 2
Line 3
Line 4
Line 5

Here instead xx00, xyz00 file is created.

5. To set number of digits after prefix(csplit -n number .. pattern):

sanfoundry-> csplit -f xyz -n 1 sample.txt 6
35
36
sanfoundry-> ls 
sample.txt  xyz0  xyz1

Here as you can see that with “-n” option you can fix the how many digits should come after the prefix, xyz. Here as we have fixed a number “1” we get xyz0 instead of xyz00.

6. To break the whole file in a specific pattern(csplit file_name pattern {*})

sanfoundry-> csplit sample.txt /Line/ {*}
0
7
7
7
7
7
7
7
7
7
8
sanfoundry-> ls
sample.txt  xx00  xx01	xx02  xx03  xx04  xx05	xx06  xx07  xx08  xx09	xx10

Here as you can see that whole file sample.txt is break down in a pattern with “{*}” option which indicates the whole file.

NOTE
All the above command line options can be used with more numbers of break points.

sanfoundry-> csplit sample.txt 2 4
7
14
50
sanfoundry-> ls
sample.txt  xx00  xx01	xx02
sanfoundry-> cat xx00
Line 1
sanfoundry-> cat xx01
Line 2
Line 3
sanfoundry-> cat xx02
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10

NOTE
The “split” command also splits a file into pieces, except that all the pieces are of a fixed size (measured in lines or bytes).

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.