$linuxjunkies
>

csplit(1)

Split a file into pieces based on context lines or line numbers.

UbuntuDebianFedoraArch

Synopsis

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

Description

csplit splits FILE into smaller files based on patterns you specify. Each pattern creates a break point, generating sequential output files (typically xx00, xx01, xx02, etc.). Patterns can match specific lines, line numbers, or regular expressions.

Unlike split which divides by fixed size, csplit divides by content context, making it ideal for separating logical sections in logs, config files, or structured text where you know the markers between sections.

Common options

FlagWhat it does
-f PREFIXuse PREFIX for output filenames instead of 'xx'
-n DIGITSuse DIGITS digits for output file numbers (default 2)
-ssuppress output file size printing
-kkeep output files even if csplit exits with an error
-qsuppress all non-error output
/REGEXP/split at lines matching regular expression
/REGEXP/OFFSETsplit at REGEXP match plus/minus OFFSET lines
LINE_NUMBERsplit at specific line number

Examples

split logfile at each ERROR line, creating multiple files for each error section

csplit logfile.txt '/ERROR/' '{*}'

split data.txt at lines 100, 200, and 300, naming output files part-00, part-01, part-02, part-03

csplit -f part- data.txt 100 200 300

split config file at each section header (lines starting with [), using 3-digit numbering

csplit -n 3 config.conf '/^\[/' '{*}'

split at lines matching ---, suppress output, keep files on error, limit to 99 splits

csplit -s -k large.txt '/^---/' '{99}'

split document into chunks when encountering Chapter or Appendix lines

csplit -f chunk_ document.txt '/^Chapter/' '/^Appendix/'

split at lines containing 2024-01 and at blank lines

csplit events.log '/2024-01-/' /^$/

Related commands