csplit -- split files according to context


csplit [ -s ] [ -k ] [ -f prefix ] [ -n number ] file arg1 [ ... argn ]


The csplit command reads file and separates it into n+1 sections, defined by the arguments arg1 ... argn. By default the sections are placed in files xx00 ... xxn (n may not be greater than 99). These sections get the following pieces of file:

From the start of file up to (but not including) the line referenced by arg1.

From the line referenced by arg1 up to the line referenced by arg2.


From the line referenced by argn to the end of file.
The options to csplit are:

csplit normally prints the character counts for each file created. If the -s option is present, csplit suppresses the printing of all character counts.

csplit normally removes created files if an error occurs. If the -k option is present, csplit leaves previously created files intact.

-f prefix
If the -f option is used, the created files are named prefix00 ... prefixn. The default value of prefix is xx.

-n number
Use number decimal digits to form filenames for the file pieces. The default is 2.
The arguments (arg1 ... argn) to csplit can be a combination of the following:

A file is to be created for the section from the current line down to (but not including) the line containing the regular expression rexp. The current line becomes the line containing rexp. This argument may be followed by an optional ``+'' or ``-'' some number of lines (for example, /Page/-5).

This argument is the same as /rexp/, except that no file is created for the section.

A file is to be created from the current line down to (but not including) lnno. The current line becomes lnno.

Repeat argument. This argument may follow any of the above arguments. If it follows an rexp-type argument, that argument is applied num more times. If it follows lnno, the file will be split every lnno lines (num times) from that point.
Enclose all rexp-type arguments that contain blanks or other characters meaningful to the shell in the appropriate quotation marks. Regular expressions may not contain embedded newlines. csplit does not affect the original file; it is the user's responsibility to remove it.

Exit values

csplit returns the following values:

successful completion

an error occurred


Self-explanatory except for:
   arg - out of range
which means that the given argument did not reference a line between the current position and the end of the file.


csplit -f cobol file  '/procedure division/'  '/par5./'  '/par16./'

This example creates four files, cobol00 ... cobol03. After editing the ``split'' files, they can be recombined as follows:

cat cobol0[0-3] > file

Note that this example overwrites the original file.

csplit -k file  100  {99}

This example would split the file at every 100 lines, up to 10,000 lines. The -k option causes the created files to be retained if there are less than 10,000 lines; however, an error message would still be printed.

csplit -k prog.c  '%main(%'  '/^}/+1'  {20}

Assuming that prog.c follows the normal C coding convention of ending routines with a } at the beginning of the line, and that main() is the first function in prog.c, this example will create a file for each separate C routine, up to 21 routines.

See also

ed(C), regex(S), sh(C)

Standards conformance

csplit is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003