Home·Terminal commands

csplit

Text processing

Usage

csplit [file] '/[pattern]/' '{*}'

Splits a file where a pattern appears rather than at a fixed size; the numbers it prints are byte counts, and {*} means repeat as often as it matches.

Common flags

FlagMeaning
-fPrefix for the output names; the default is xx
-nHow many digits the number has
-kKeep the files already written if it fails partway
-sDo not print the byte count of each piece
-zGNU: throw away pieces that came out empty
{*}Repeat the pattern as many times as it matches

Examples

csplit -z -f part_ log.txt '/^ERROR/' '{*}'

A new file at every ERROR line.

csplit book.txt 100 200

Splits at lines 100 and 200.

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does csplit do?

Splits a file where a pattern appears rather than at a fixed size; the numbers it prints are byte counts, and {*} means repeat as often as it matches.

Q. How do I type it?

csplit [file] '/[pattern]/' '{*}' — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man csplit. This command sits under Text processing.

Related commands

man page: man csplit