Linux csplit command

Updated: 11/06/2021 by Computer Hope
csplit command

On Unix-like operating systems, the csplit command splits a file into sections, based on context lines.

This page covers the GNU/Linux version of ucsplit.

Description

csplit outputs pieces of FILE separated by PATTERN(s) to files 'xx00', 'xx01', ..., and output byte counts of each piece to standard output.

Syntax

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

Options

-b,
--suffix-format=FORMAT
Use sprintf FORMAT instead of %02d.
-f, --prefix=PREFIX Use PREFIX instead of 'xx'.
-k, --keep-files Do not remove output files on errors.
-n, --digits=DIGITS Use specified number of digits instead of 2.
-s, --quiet, --silent Do not print counts of output file sizes.
-z, --elide-empty-files Remove empty output files.
--help Display a help message and exit.
--version Output version information and exit.

csplit reads standard input if FILE is specified as a dash ("-"). Each PATTERN may be:

INTEGER Copy up to but not including specified line number.
/REGEXP/[OFFSET] Copy up to but not including a matching line.
%REGEXP%[OFFSET] Skip to, but not including a matching line.
{INTEGER} Repeat the previous pattern specified number of times.
{*} Repeat the previous pattern as often as possible.

A line OFFSET is a required '+' or '-' followed by a positive integer.

Examples

csplit -f cobol filename '/procedure division/' /par5./ /par16./

Creates four files, cobol00...cobol03.

After editing the split files, they can be recombined into filename using the cat command as follows:

cat cobol0[0-3] > filename

cat — Output the contents of a file.
sed — A utility for filtering and transforming text.
split — Split a file into pieces.