LinuxCommandLibrary

csplit

TLDR

Split a file starting second part at line 10

$ csplit [path/to/file] 10
copy
Split a file into three parts at lines 7 and 23
$ csplit [path/to/file] 7 23
copy
Start a new part at every 5th line
$ csplit [path/to/file] 5 {*}
copy
Split every 5th line, ignoring division errors
$ csplit -k [path/to/file] 5 {*}
copy
Use a custom prefix for output files
$ csplit [path/to/file] 5 -f [prefix]
copy
Split above first line matching a regex
$ csplit [path/to/file] /[regex]/
copy

SYNOPSIS

csplit [options] file pattern...

DESCRIPTION

csplit splits a file into pieces based on context (line numbers or patterns). It generates files named xx00, xx01, etc. by default.
Unlike split which divides by size, csplit divides by content structure, making it useful for splitting log files, configuration files, or documents at specific boundaries.

PARAMETERS

-f, --prefix prefix

Use specified prefix instead of 'xx'
-k, --keep-files
Don't remove output files on error
-n, --digits digits
Number of digits in output filename
-s, --quiet
Don't print byte counts
{n}
Repeat previous pattern n times
**{*}**
Repeat previous pattern as many times as possible

CAVEATS

Part of GNU coreutils. Without -k, output files are deleted if an error occurs. Pattern repetition with {*} will fail if the pattern doesn't divide the file evenly (use -k to keep partial output).

SEE ALSO

split(1), cut(1), head(1)

Copied to clipboard