xref: /freebsd/usr.bin/csplit/csplit.1 (revision a0ee8cc6)
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.\" $FreeBSD$
26.\"
27.Dd February 6, 2014
28.Dt CSPLIT 1
29.Os
30.Sh NAME
31.Nm csplit
32.Nd split files based on context
33.Sh SYNOPSIS
34.Nm
35.Op Fl ks
36.Op Fl f Ar prefix
37.Op Fl n Ar number
38.Ar file args ...
39.Sh DESCRIPTION
40The
41.Nm
42utility splits
43.Ar file
44into pieces using the patterns
45.Ar args .
46If
47.Ar file
48is
49a dash
50.Pq Sq Fl ,
51.Nm
52reads from standard input.
53.Pp
54Files are created with a prefix of
55.Dq xx
56and two decimal digits.
57The size of each file is written to standard output
58as it is created.
59If an error occurs whilst files are being created,
60or a
61.Dv HUP ,
62.Dv INT ,
63or
64.Dv TERM
65signal is received,
66all files previously written are removed.
67.Pp
68The options are as follows:
69.Bl -tag -width indent
70.It Fl f Ar prefix
71Create file names beginning with
72.Ar prefix ,
73instead of
74.Dq Pa xx .
75.It Fl k
76Do not remove previously created files if an error occurs or a
77.Dv HUP ,
78.Dv INT ,
79or
80.Dv TERM
81signal is received.
82.It Fl n Ar number
83Create file names beginning with
84.Ar number
85of decimal digits after the prefix,
86instead of 2.
87.It Fl s
88Do not write the size of each output file to standard output as it is
89created.
90.El
91.Pp
92The
93.Ar args
94operands may be a combination of the following patterns:
95.Bl -tag -width indent
96.It Xo
97.Sm off
98.Cm / Ar regexp Cm / Op Oo Cm + | - Oc Ar offset
99.Sm on
100.Xc
101Create a file containing the input from the current line to (but not including)
102the next line matching the given basic regular expression.
103An optional
104.Ar offset
105from the line that matched may be specified.
106.It Xo
107.Sm off
108.Cm % Ar regexp Cm % Op Oo Cm + | - Oc Ar offset
109.Sm on
110.Xc
111Same as above but a file is not created for the output.
112.It Ar line_no
113Create containing the input from the current line to (but not including)
114the specified line number.
115.It Cm { Ns Ar num Ns Cm }
116Repeat the previous pattern the specified number of times.
117If it follows a line number pattern, a new file will be created for each
118.Ar line_no
119lines,
120.Ar num
121times.
122The first line of the file is line number 1 for historic reasons.
123.El
124.Pp
125After all the patterns have been processed, the remaining input data
126(if there is any) will be written to a new file.
127.Pp
128Requesting to split at a line before the current line number or past the
129end of the file will result in an error.
130.Sh ENVIRONMENT
131The
132.Ev LANG , LC_ALL , LC_COLLATE
133and
134.Ev LC_CTYPE
135environment variables affect the execution of
136.Nm
137as described in
138.Xr environ 7 .
139.Sh EXIT STATUS
140.Ex -std
141.Sh EXAMPLES
142Split the
143.Xr mdoc 7
144file
145.Pa foo.1
146into one file for each section (up to 21 plus one for the rest, if any):
147.Pp
148.Dl "csplit -k foo.1 '%^\e.Sh%' '/^\e.Sh/' '{20}'"
149.Pp
150Split standard input after the first 99 lines and every 100 lines thereafter:
151.Pp
152.Dl "csplit -k - 100 '{19}'"
153.Sh SEE ALSO
154.Xr sed 1 ,
155.Xr split 1 ,
156.Xr re_format 7
157.Sh STANDARDS
158The
159.Nm
160utility conforms to
161.St -p1003.1-2001 .
162.Sh HISTORY
163A
164.Nm
165command appeared in PWB UNIX.
166.Sh BUGS
167Input lines are limited to
168.Dv LINE_MAX
169(2048) bytes in length.
170