1.\" $OpenBSD: csplit.1,v 1.13 2022/12/22 19:53:22 kn Exp $ 2.\" 3.\" Copyright (c) 2002 Tim J. Robbins. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" $FreeBSD: src/usr.bin/csplit/csplit.1,v 1.11 2005/01/25 22:29:51 tjr Exp $ 28.\" 29.Dd $Mdocdate: December 22 2022 $ 30.Dt CSPLIT 1 31.Os 32.Sh NAME 33.Nm csplit 34.Nd split files based on context 35.Sh SYNOPSIS 36.Nm 37.Op Fl ks 38.Op Fl f Ar prefix 39.Op Fl n Ar number 40.Ar file 41.Ar arg ... 42.Sh DESCRIPTION 43The 44.Nm 45utility splits 46.Ar file 47into pieces using the pattern 48.Ar arg . 49If 50.Ar file 51is 52a dash 53.Pq Sq - , 54.Nm 55reads from standard input. 56.Pp 57Files are created with a prefix of 58.Dq xx 59and two decimal digits. 60The size of each file is written to standard output 61as it is created. 62If an error occurs whilst files are being created, 63or a 64.Dv HUP , 65.Dv INT , 66or 67.Dv TERM 68signal is received, 69all files previously written are removed. 70.Pp 71The options are as follows: 72.Bl -tag -width indent 73.It Fl f Ar prefix 74Create file names beginning with 75.Ar prefix , 76instead of 77.Dq xx . 78.It Fl k 79Do not remove previously created files if an error occurs or a 80.Dv HUP , 81.Dv INT , 82or 83.Dv TERM 84signal is received. 85.It Fl n Ar number 86Create file names beginning with 87.Ar number 88of decimal digits after the prefix, 89instead of 2. 90.It Fl s 91Do not write the size of each output file to standard output as it is 92created. 93.El 94.Pp 95The 96.Ar arg 97operand may be a combination of the following patterns: 98.Bl -tag -width indent 99.It Xo 100.Sm off 101.No / Ar regexp No / 102.Op Oo Cm + | - Oc Ar offset 103.Sm on 104.Xc 105Create a file containing the input from the current line to (but not including) 106the next line matching the given basic regular expression. 107An optional 108.Ar offset 109from the line that matched may be specified. 110.It Xo 111.Sm off 112.No % Ar regexp No % 113.Op Oo Cm + | - Oc Ar offset 114.Sm on 115.Xc 116Same as above but a file is not created for the output. 117.It Ar line_no 118Create containing the input from the current line to (but not including) 119the specified line number. 120.It { Ns Ar num Ns } 121Repeat the previous pattern the specified number of times. 122If it follows a line number pattern, a new file will be created for each 123.Ar line_no 124lines, 125.Ar num 126times. 127The first line of the file is line number 1 for historic reasons. 128.El 129.Pp 130After all the patterns have been processed, the remaining input data 131(if there is any) will be written to a new file. 132.Pp 133Requesting to split at a line before the current line number or past the 134end of the file will result in an error. 135.Sh EXIT STATUS 136.Ex -std csplit 137.Sh EXAMPLES 138Split the 139.Xr mdoc 7 140file 141.Pa foo.1 142into one file for each section (up to 21 files): 143.Pp 144.Dl "$ csplit -k foo.1 '%^\e.Sh%' '/^\e.Sh/' '{20}'" 145.Pp 146Split standard input after the first 99 lines and every 100 lines 147thereafter (up to 21 files): 148.Pp 149.Dl "$ csplit -k - 100 '{19}'" 150.Sh SEE ALSO 151.Xr sed 1 , 152.Xr split 1 , 153.Xr re_format 7 154.Sh STANDARDS 155The 156.Nm 157utility is compliant with the 158.St -p1003.1-2008 159specification. 160.Sh HISTORY 161A 162.Nm 163command appeared in PWB 164.Ux . 165.Sh BUGS 166Input lines are limited to 167.Dv LINE_MAX 168(2048) bytes in length. 169