xref: /freebsd/usr.bin/split/split.1 (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1.\" Copyright (c) 1990, 1991, 1993, 1994
2.\"	The Regents of the University of California.  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.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd May 26, 2023
29.Dt SPLIT 1
30.Os
31.Sh NAME
32.Nm split
33.Nd split a file into pieces
34.Sh SYNOPSIS
35.Nm
36.Op Fl cd
37.Op Fl l Ar line_count
38.Op Fl a Ar suffix_length
39.Op Ar file Op Ar prefix
40.Nm
41.Op Fl cd
42.Fl b Ar byte_count Ns
43.Oo
44.Sm off
45.Cm K | k | M | m | G | g
46.Sm on
47.Oc
48.Op Fl a Ar suffix_length
49.Op Ar file Op Ar prefix
50.Nm
51.Op Fl cd
52.Fl n Ar chunk_count
53.Op Fl a Ar suffix_length
54.Op Ar file Op Ar prefix
55.Nm
56.Op Fl cd
57.Fl p Ar pattern
58.Op Fl a Ar suffix_length
59.Op Ar file Op Ar prefix
60.Sh DESCRIPTION
61The
62.Nm
63utility reads the given
64.Ar file
65and breaks it up into files of 1000 lines each
66(if no options are specified), leaving the
67.Ar file
68unchanged.
69If
70.Ar file
71is a single dash
72.Pq Sq Fl
73or absent,
74.Nm
75reads from the standard input.
76.Pp
77The options are as follows:
78.Bl -tag -width indent
79.It Fl a Ar suffix_length
80Use
81.Ar suffix_length
82letters to form the suffix of the file name.
83.It Fl b Ar byte_count Ns Oo
84.Sm off
85.Cm K | k | M | m | G | g
86.Sm on
87.Oc
88Create split files
89.Ar byte_count
90bytes in length.
91If
92.Cm k
93or
94.Cm K
95is appended to the number, the file is split into
96.Ar byte_count
97kilobyte pieces.
98If
99.Cm m
100or
101.Cm M
102is appended to the number, the file is split into
103.Ar byte_count
104megabyte pieces.
105If
106.Cm g
107or
108.Cm G
109is appended to the number, the file is split into
110.Ar byte_count
111gigabyte pieces.
112.It Fl c
113Continue creating files and do not overwrite existing
114output files.
115.It Fl d
116Use a numeric suffix instead of a alphabetic suffix.
117.It Fl l Ar line_count
118Create split files
119.Ar line_count
120lines in length.
121.It Fl n Ar chunk_count
122Split file into
123.Ar chunk_count
124smaller files.
125The first n - 1 files will be of size (size of
126.Ar file
127/
128.Ar chunk_count
129)
130and the last file will contain the remaining bytes.
131.It Fl p Ar pattern
132The file is split whenever an input line matches
133.Ar pattern ,
134which is interpreted as an extended regular expression.
135The matching line will be the first line of the next output file.
136This option is incompatible with the
137.Fl b
138and
139.Fl l
140options.
141.El
142.Pp
143If additional arguments are specified, the first is used as the name
144of the input file which is to be split.
145If a second additional argument is specified, it is used as a prefix
146for the names of the files into which the file is split.
147In this case, each file into which the file is split is named by the
148prefix followed by a lexically ordered suffix using
149.Ar suffix_length
150characters in the range
151.Dq Li a Ns - Ns Li z .
152If
153.Fl a
154is not specified, two letters are used as the initial suffix.
155If the output does not fit into the resulting number of files and the
156.Fl d
157flag is not specified, then the suffix length is automatically extended as
158needed such that all output files continue to sort in lexical order.
159.Pp
160If the
161.Ar prefix
162argument is not specified, the file is split into lexically ordered
163files named with the prefix
164.Dq Li x
165and with suffixes as above.
166.Pp
167By default,
168.Nm
169will overwrite any existing output files.
170If the
171.Fl c
172flag is specified,
173.Nm
174will instead create files with names that do not already exist.
175.Sh ENVIRONMENT
176The
177.Ev LANG , LC_ALL , LC_CTYPE
178and
179.Ev LC_COLLATE
180environment variables affect the execution of
181.Nm
182as described in
183.Xr environ 7 .
184.Sh EXIT STATUS
185.Ex -std
186.Sh EXAMPLES
187Split input into as many files as needed, so that each file contains at most 2
188lines:
189.Bd -literal -offset indent
190$ echo -e "first line\\nsecond line\\nthird line\\nforth line" | split -l2
191.Ed
192.Pp
193Split input in chunks of 10 bytes using numeric prefixes for file names.
194This generates two files of 10 bytes (x00 and x01) and a third file (x02) with the
195remaining 2 bytes:
196.Bd -literal -offset indent
197$ echo -e "This is 22 bytes long" | split -d -b10
198.Ed
199.Pp
200Split input generating 6 files:
201.Bd -literal -offset indent
202$ echo -e "This is 22 bytes long" | split -n 6
203.Ed
204.Pp
205Split input creating a new file every time a line matches the regular expression
206for a
207.Dq t
208followed by either
209.Dq a
210or
211.Dq u
212thus creating two files:
213.Bd -literal -offset indent
214$ echo -e "stack\\nstock\\nstuck\\nanother line" | split -p 't[au]'
215.Ed
216.Sh SEE ALSO
217.Xr csplit 1 ,
218.Xr re_format 7
219.Sh STANDARDS
220The
221.Nm
222utility conforms to
223.St -p1003.1-2001 .
224.Sh HISTORY
225A
226.Nm
227command appeared in
228.At v3 .
229.Pp
230Before
231.Fx 14 ,
232pattern and line matching only operated on lines shorter than 65,536 bytes.
233