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.
csplit [OPTION]... FILE PATTERN...
Try it
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.
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
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
xx00is then an empty file unless you use-z. - Use
{*}to cut at every match. A number such asrepeats 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
grepwithout-E. Write\+and\{n\}for repeats. - The count printout goes to standard output; with
-snothing 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.
awkcan write each section to its own file with a single rule like/^==/ {n++} {print > "sec" n}, and gives more control over names.