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