Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux csplit command

Reference for the Linux csplit command: split a file at line numbers or regular expression matches, repeat patterns, name pieces and suppress the size output.

csplit splits a file wherever the content says so. You give it a list of patterns: a line number, or a regular expression written between slashes. Each pattern ends the current piece, and the matching line starts the next one. The result is files named xx00, xx01, ... plus, by default, a printout of each piece's size in bytes.

Use it for text that has natural section boundaries: chapters starting with a heading, log files with a marker line per run, or concatenated documents. For fixed sizes use split instead.

Terminal
csplit [OPTION]... FILE PATTERN...

Try it

Bash Split at headings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Line numbers, offsets and sizes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
csplit [-s] [-k] [-z] [-f PREFIX] [-n DIGITS] [-b FORMAT] FILE PATTERN...

FILE may be - for standard input.

Pattern Meaning
N Split before line number N (so the piece ends at line N-1).
/REGEX/ Split before the next line that matches REGEX.
/REGEX/+N, /REGEX/-N Split N lines after or before that match.
%REGEX% Skip to the matching line without writing the skipped lines.
After a pattern: repeat it N more times.
{*} Repeat the pattern as many times as it matches.
Option Description
-f PREFIX File name prefix (default xx).
-n DIGITS Number of digits in the suffix (default 2).
-b FORMAT, --suffix-format=FORMAT A printf-style format for the suffix, such as %03d.txt.
-s, -q Do not print piece sizes.
-k Keep pieces already written if an error occurs.
-z Remove empty pieces.

Returns: 0 on success. If a pattern has no match (for example a plain /x/ when there is no x in the file; {*} stops quietly) csplit reports match not found and, unless -k is given, removes the files it created.

Usage notes

  • The matching line starts the next piece. For a heading pattern this is what you want. The first piece holds everything before the first match; it is empty when the file starts with a match, and xx00 is then an empty file unless you use -z.
  • Use {*} to cut at every match. A number such as repeats the pattern twice more, so it is used three times in total.
  • Pattern quoting. Quote patterns so the shell does not expand *, { or ?: '/^==/' '{*}'.
  • Regular expressions are POSIX basic regular expressions, like grep without -E. Write \+ and \{n\} for repeats.
  • The count printout goes to standard output; with -s nothing is printed, which is best in scripts.
  • No overwrite check. Existing files with the same names are replaced.
  • Line numbers are absolute. Several line patterns must be increasing.
  • Alternatives. awk can write each section to its own file with a single rule like /^==/ {n++} {print > "sec" n}, and gives more control over names.