csplit(1)
Split a file into pieces based on context lines or line numbers.
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
| Flag | What it does |
|---|---|
-f PREFIX | use PREFIX for output filenames instead of 'xx' |
-n DIGITS | use DIGITS digits for output file numbers (default 2) |
-s | suppress output file size printing |
-k | keep output files even if csplit exits with an error |
-q | suppress all non-error output |
/REGEXP/ | split at lines matching regular expression |
/REGEXP/OFFSET | split at REGEXP match plus/minus OFFSET lines |
LINE_NUMBER | split 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 300split 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-/' /^$/