csplit Command

Purpose

Splits a file into individual files.

Syntax

csplit-f Prefix ] [  -k ] [  -n Number ] [  -s ] File Argument ...

Description

The csplit command copies the specified file and separates the copy into segments. The original input file, which remains unaltered, must be a text file.

The csplit command writes the segments to files xx00 . . . xx99, depending on how many times the Argument parameter is specified (99 is the maximum). By default, the Argument parameter expects a line number. The following rules apply when you specify multiple line numbers:

For example, if the original file had 108 lines and you entered:

csplit orginal.txt 11 72 98

the csplit command would create four files: the xx00 file would contain lines 1-10, the xx01 file would contain lines 11-71, the xx02 file would contain lines 72-97, the xx03 file would contain lines 98-108.

The Argument parameter can also contain the following symbols and pattern strings:

Item Description
/Pattern/ Creates a file that contains the segment from the current line up to, but not including, the line containing the specified pattern. The line containing the pattern becomes the current line.
%Pattern% Makes the line containing the specified pattern the current line, but does not create a file for the segment.
+Number Moves forward the specified number of lines from the line matched by the preceding pattern. For example, /Page/+5 searches for Page, then advances 5 lines.
-Number Moves backward the specified number of lines from the line matched by the preceding pattern. For example, /Page/-5 searches for Page, then backs up 5 lines.
{Number} Repeats the preceding option the specified number of times. This number can follow any pattern or line number. If it follows a pattern, the csplit command reuses that pattern the specified number of times. If it follows a line number, the csplit command splits the file from that point for the number of lines specified by the line number.

Put quotation marks around all patterns that contain spaces or other characters special to the shell. Patterns may not contain embedded new-line characters. In an expression such as [a-z], the - (minus sign) means through, according to the current collating sequence. A collating sequence may define equivalence classes for use in character ranges.

Flags

Item Description
-f Prefix Specifies the prefix to be used for the created file segments. The default value for this variable is xx.
-k Leaves created file segments intact in the event of an error.
-n Number Changes the number of decimal places used in the created file names. The default is two decimal places, or xx00 . . . xx99. If you specify the -n 4 flag, for example, new files are named xx0000 . . . xx0099.
-s Suppresses the display of character counts.

Exit Status

This command returns the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. To split the text of book into a separate file for each chapter, enter:
    csplit book "/^ Chapter *[k.0-9]k./" {9}
    This creates 10 files, xx00 through xx09. The xx00 file contains the front matter that comes before the first chapter. Files xx01 through xx09 contain individual chapters. Each chapter begins with a line that contains only the word Chapter and the chapter number.
  2. To specify the prefix chap for the files created from book, enter:

    csplit  -f chap book "/^ Chapter *[k.0-9]k./" {9}

    This splits book into files named chap00 through chap09.

Files

Item Description
/usr/bin/csplit Contains the csplit command.