maux
Star on GitHub Buy me a coffee

Manual / csplit

csplit(1)

CSPLIT(1) General Commands Manual CSPLIT(1)

csplit - split a file into pieces at context lines

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

Csplit reads file and writes it out in pieces at the boundaries named by each arg, processed in order, current position starting at line 1:

A boundary right before that (1-based) line. number equal to the current line makes an empty piece; number less than it is an error.
/bre/[+-offset]
Search forward for the next line matching bre; the boundary falls offset lines (default 0) past it. The matched-through span becomes a real piece file.
%bre%[+-offset]
The same search, but the span is discarded — no piece file is created for it.
{n}
Repeat the preceding bre argument n more times, each a fresh forward search from wherever the last one left off. ({*} is a KNOWN GAP: the repeat count must be a decimal number.)

Whatever remains after the last arg becomes one final piece. Pieces are named prefix (-f, default xx) followed by a zero-padded decimal counter (-n digits, default 2): xx00, xx01, and so on. Each piece's byte count is written to standard output, one line, right after it closes, unless -s.

A bre that never matches before end of file, or a number not strictly greater than the current line, are errors: without -k, every piece this invocation created (the partial one included) is destroyed and the run exits 1; with -k, they stay. Offset is honored only when positive (KNOWN LIMITATION: a negative offset needs a multi-line lookback buffer this build does not carry, so it is accepted syntactically and treated as 0). The delimiter character's first later occurrence ends the pattern; there is no \/\% escape.

/usr/man/CSPLIT this page

Exit status is 0 on success; 1 if the input file could not be opened, the prefix/digit-width exceeds 15 characters, a piece could not be created or written, or an arg failed to match/order as described above; 2 on a usage error.

None known.

September 15, 2026 maux