get rid of unmatched quote so ANSI cpp doesn't barf
[unix-history] / usr / src / old / sed / sed.1
CommitLineData
7701b0dd 1.\" Copyright (c) 1991 The Regents of the University of California.
94bed826 2.\" All rights reserved.
2d33ba58 3.\"
ae122740
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
7701b0dd 7.\" %sccs.include.redist.roff%
94bed826 8.\"
c3fc0f23 9.\" @(#)sed.1 6.7 (Berkeley) %G%
daacd5f0 10.\"
7701b0dd
CL
11.Dd
12.Dt SED 1
13.Os
14.Sh NAME
15.Nm sed
16.Nd stream editor.
17.Sh SYNOPSIS
18.Nm sed
19.Op Fl n
20.Ar script
21.Ar
22.Nm sed
23.Op Fl n
24.Op Fl e Ar script
25.Op Fl f Ar script_file
26.Ar
27.Sh DESCRIPTION
2d33ba58 28The
7701b0dd
CL
29.Nm sed
30utility is a stream editor which reads one or more
31text files, applies given editing command scripts,
32and writes the results to standard output.
33The script of editing commands can be given in the command
34line, or can be contained in the file
35.Ar script_file .
36.Pp
37Options:
38.Bl -tag -width indent
39.It Fl e Ar script
40The command line
41.Ar script
42is used to edit the input.
43If multiple
44.Fl e
45options are given, the scripts are
46applied in the order given to each line of the
47input files.
48If a
49.Fl f
50option is given in addition
51to
52.Fl e ,
53lines are acted upon by scripts first.
54.It Fl f Ar script_file
55The file
56.Ar script_file
57is expected to contain editing commands, one per line,
58and these commands are applied to the input.
59If multiple
60.Fl f
61options are given, the commands in the
62.Ar script_file Ns s
63are applied in
64the order given to each line of the input
65files.
66If a
67.Fl e
68option is given in addition to
69.Fl f ,
70lines are acted upon by
71the commands in the
72.At script_file Ns s
73first.
74.It Fl n
2d33ba58 75The
7701b0dd
CL
76.Fl n
77option suppresses the default output, which normally
78passes each line, after it is examined for editing,
79to standard output.
80Therefore, only lines explicitly
81selected for output are written.
82.El
83.Pp
84The following operands are available:
85.Bl -tag -width file
86.It Ar file
87A pathname of a file whose contents are read and
88edited.
89If multiple file operands are given,
90the named files are read in the order given and
91the concatenation is edited.
92If no file operands
93are given, the standard input is used.
94.It Ar script
95The script consists of one or more editing
96instructions that are entered on the command line.
97.El
98.\" .Pp
99.\" The following environment variable affects the execution of
100.\" sed:
101.\" .It Ev LC_CTYPE
102.\" The locale for character classification.
103.Pp
104.Bd -filled -offset indent -compact
105.Op address Op ,address
106function
107.Op arguments
108.Ed
109.Pp
110In default operation,
111.Nm sed
112cyclically copies a line of input
113into a pattern space (unless there is something left after a
114.Cm D
115command), applies in sequence all commands whose addresses
116select that pattern space, and at the end of the script
117copies the pattern space to the standard output (except
118under
119.Fl n )
2d33ba58 120and deletes the pattern space.
7701b0dd
CL
121.Pp
122Some of the commands use a hold space to save all or part of
123the pattern space for
124subsequent retrieval.
125.\" The pattern and hold spaces are each
126.\" limited to
127.\" .Pf { Dv SED_PATTERN_MAX Ns }
128.\" bytes.
129.Pp
130An address is either no address; a decimal number that
131counts input lines cumulatively across files; a
132.Ql $
133that
134addresses the last line of input; a context address; or
135regular expression.
136.Pp
137A command line with no addresses selects every pattern
138space.
139.Pp
140A command line with one address selects each pattern space
141that matches the address.
142.Pp
143A command line with two addresses selects the inclusive
144range from the first pattern space that matches the first
145address through the next pattern space which matches the
146second.
147(If the second address is a number less than or
148equal to the line number first selected, only one line is
149selected.)
150Starting at the first line following the
151selected range,
152.Nm sed
153looks again for the first address.
154Thereafter the process is repeated.
155.Pp
2d33ba58 156Editing commands can be applied only to non-selected pattern
7701b0dd
CL
157spaces by use of the negation function
158.Cm \&!
159(See below.)
160.Pp
161The
162.Nm sed
163utility uses basic regular expressions, as are found in the
164editor
165.Xr ed 1 ,
166with the following additions:
167.Pp
168.Bl -enum -offset indent
169.It
c3fc0f23
KB
170In a context address, the construction
171.Li \e?RE? ,
172where ?
173is any character, is identical to
174.Li /RE/ .
175Note that in the
176context address
177.Li \exabc\exdefx ,
178the second
179.Ql x
180stands for
181itself, so that the regular expression is
182.Li abcxdef .
7701b0dd 183.It
c3fc0f23
KB
184The escape sequence
185.Ql \en
186matches a <newline> embedded
7701b0dd
CL
187in the pattern space.
188.It
c3fc0f23
KB
189A period
190.Ql \&.
191matches any character except the terminal <newline> of the pattern space.
7701b0dd
CL
192.El
193.Pp
194In the following list of functions the maximum number of
195permissible addresses for each function is indicated by
196.Op 0addr ,
197.Op 1addr ,
2d33ba58 198or
7701b0dd
CL
199.Op 2addr ;
200representing zero, one, or two
201addresses.
202.Pp
203The argument text consists of one or more lines.
204Each
205embedded <newline> in the text shall be preceded by a
206backslash.
207Other backslashes in text are treated like
208backslashes in the replacement string of an s command, and
209can be used to protect initial <blank>s against the stripping
210that is done on every script line.
211.Pp
212The
213.Cm r
214and
215.Cm w
216commands take an optional
217.Ar rfile
218(or
219.Ar wfile )
220parameter, separated from the command letter by zero or more
221<blank>s.
222.Pp
223The argument
224.Ar rfile
225or the argument
226.Ar wfile
227shall terminate the
228command line.
2d33ba58 229Each
7701b0dd
CL
230.Ar wfile
231is created before processing
232begins.
233There can be at most ten distinct
234.Ar wfile
235arguments
236in the script.
237.Pp
238The
239.Cm b , r , s , t ,
240.Cm w , y , \&! ,
241and
242.Cm \&:
243commands take additional
2d33ba58 244arguments.
7701b0dd
CL
245The following synopses indicate which arguments
246are separated from the commands by <blank>s.
247.Bl -tag -width addrcommandxx
c3fc0f23
KB
248.It Xo
249.Oo Ad 2addr Oc \&{ command_list \&}
250.Xc
7701b0dd
CL
251Executes command_list only when the pattern
252space is selected.
253The {} braces can be preceded
254and followed by white space.
c3fc0f23
KB
255.It Xo
256.Oo Ad 1addr Oc Ns Cm a Ar text
257.Xc
7701b0dd
CL
258Writes text to the standard output after the
259pattern space is written.
c3fc0f23
KB
260.It Xo
261.Oo Ad 2addr Oc Ns Cm b Ar label
262.Xc
7701b0dd
CL
263Branches to the
264.Cm \&:
265command bearing the label.
266If label is empty, branch to the end of the
267script.
c3fc0f23
KB
268.It Xo
269.Oo Ad 2addr Oc Ns Cm c Ar text
270.Xc
7701b0dd
CL
271Deletes the pattern space.
272With 0 or 1
273address or at the end of a 2-address range,
274places text on the output.
c3fc0f23
KB
275.It Xo
276.Oo Ad 2addr Oc Ns Cm d
277.Xc
7701b0dd
CL
278Deletes the pattern space and starts the next
279cycle.
c3fc0f23
KB
280.It Xo
281.Oo Ad 2addr Oc Ns Cm D
282.Xc
7701b0dd
CL
283Deletes the initial segment of the pattern
284space through the first <newline> and starts
285the next cycle.
c3fc0f23
KB
286.It Xo
287.Oo Ad 2addr Oc Ns Cm g
288.Xc
7701b0dd
CL
289Replaces the contents of the pattern space by
290the contents of the hold space.
c3fc0f23
KB
291.It Xo
292.Oo Ad 2addr Oc Ns Cm G
293.Xc
7701b0dd
CL
294Appends the contents of the hold space to the
295pattern space.
c3fc0f23
KB
296.It Xo
297.Oo Ad 2addr Oc Ns Cm h
298.Xc
7701b0dd
CL
299Replaces the contents of the hold space by
300the contents of the pattern space.
c3fc0f23
KB
301.It Xo
302.Oo Ad 2addr Oc Ns Cm H
303.Xc
7701b0dd
CL
304Appends the contents of the pattern space to
305the hold space.
c3fc0f23
KB
306.It Xo
307.Oo Ad 1addr Oc Ns Cm i Ar text
308.Xc
7701b0dd
CL
309Writes text to the standard output before the
310pattern space is written.
c3fc0f23
KB
311.It Xo
312.Oo Ad 2addr Oc Ns Cm l
313.Xc
7701b0dd
CL
314Lists the pattern space on the standard out-
315put in an unambiguous form.
316Nonprinting
317characters are listed as hexadecimal digit
318pairs, with a preceding backslash, with the
319following exceptions:
c3fc0f23 320.Bl -column <carriagexreturn> -offset indent
7701b0dd
CL
321<alert> \ea
322<backslash> \e\e
323<backspace> \eb
324<carriage return> \er
325<form-feed> \ef
326<newline> \en
327<tab> \et
328<vertical tab> \ev
329.El
7701b0dd
CL
330.Pp
331Long lines are folded; the length at which
332folding occurs is ungiven, but should be
333appropriate for the output device.
c3fc0f23
KB
334.It Xo
335.Oo Ad 2addr Oc Ns Cm n
336.Xc
7701b0dd
CL
337Copies the pattern space to the standard output
338and replaces the pattern space with the
339next line of input.
c3fc0f23
KB
340.It Xo
341.Oo Ad 2addr Oc Ns Cm N
342.Xc
7701b0dd
CL
343Appends the next line of input to the pattern
344space, using an embedded <newline> to
345separate the appended material from the
346original material.
347Note that the current line
348number changes.
c3fc0f23
KB
349.It Xo
350.Oo Ad 2addr Oc Ns Cm p
351.Xc
7701b0dd
CL
352Copies
353.Op prints
354the pattern space to the
355standard output.
c3fc0f23
KB
356.It Xo
357.Oo Ad 2addr Oc Ns Cm P
358.Xc
7701b0dd
CL
359Copies
360.Op prints
361the pattern space, up to the
362first <newline>, to the standard output.
c3fc0f23
KB
363.It Xo
364.Oo Ad 1addr Oc Ns Cm q
365.Xc
7701b0dd
CL
366Branches to the end of the script and quits
367without starting a new cycle.
c3fc0f23
KB
368.It Xo
369.Oo Ad 1addr Oc Ns Cm r Ar rfile
370.Xc
7701b0dd
CL
371Read the contents of rfile.
372Place them on the
373output before reading the next input line.
c3fc0f23
KB
374.It Xo
375.Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags
376.Xc
7701b0dd
CL
377Substitutes the replacement string for
378instances of the regular expression in the
379pattern space.
380Any character can be used
381instead of
382.Ql / .
383The value of flags is zero or
384more of:
385.Bl -tag -width Ds
386.It Ar n
c3fc0f23
KB
387n=1-512. Substitutes for the
388.Ar n Ns th
7701b0dd
CL
389occurrence only of the regular
390expression found within the pattern space.
391.It Cm g
392Globally substitutes for all
393non-overlapping instances of the regular
394expression rather than just
395the first one. If both
396.Cm g
397and
398.Cm n
399are given,
400.Cm g
401takes precedence.
402.It Cm p
403Prints the pattern space if a
404replacement was made.
405.It Cm w Ar wfile
406Write. Appends the pattern space
407to
408.Ar wfile
409if a replacement was
410made.
411.El
c3fc0f23
KB
412.It Xo
413.Oo Ad 2addr Oc Ns Cm t Ar label
414.Xc
2d33ba58 415Test.
7701b0dd
CL
416Branches to the
417.Cm \&:
418command bearing the
419label if any substitutions have been made
420since the most recent reading of an input
421line or execution of a
422.Cm t .
423If label is empty,
424branches to the end of the script.
c3fc0f23
KB
425.It Xo
426.Oo Ad 2addr Oc Ns Cm w Ar wfile
427.Xc
7701b0dd
CL
428Appends
429.Op writes
430the pattern space to
431.Ar wfile .
c3fc0f23
KB
432.It Xo
433.Oo Ad 2addr Oc Ns Cm x
434.Xc
7701b0dd
CL
435Exchanges the contents of the pattern and
436hold spaces.
c3fc0f23
KB
437.It Xo
438.Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/
439.Xc
7701b0dd
CL
440Replaces all occurrences of collating
441elements in
442.Ar string1
443with the corresponding
444collating element in
445.Ar string2 .
2d33ba58 446The lengths of
7701b0dd 447.Ar string1
2d33ba58 448and
7701b0dd
CL
449.Ar string2
450shall be equal.
c3fc0f23
KB
451.It Xo
452.Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction
453.Xc
7701b0dd
CL
454Applies the function (or group, if function
455is {) only to the lines that are not selected
456by the address(es).
c3fc0f23
KB
457.It Xo
458.Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel
459.Xc
7701b0dd
CL
460This command does nothing; it bears a label
461for the b and t commands to branch to.
c3fc0f23
KB
462.It Xo
463.Oo Ad 1addr Oc Ns Cm \&=
464.Xc
7701b0dd
CL
465Places the current line number on the standard
466output as a line with its own line
467number.
c3fc0f23
KB
468.It Xo
469.Oo Ad 0addr Oc
470.Xc
2d33ba58 471An empty command is ignored.
c3fc0f23
KB
472.It Xo
473.Oo Ad 0addr Oc Cm #
474.Xc
7701b0dd
CL
475If a
476.Cm #
477appears as the first character on any
478line of a script file, that entire line is
479ignored (treated as a comment), with the single
480exception that if the first line of the
481script file begins with
482.Cm Ns #n ,
483the default
484output is suppressed.
485.El
486.Pp
487The
488.Nm sed
489utility exits 0 on success, and >0 if an error occurs.
490.Pp
491If one or more of the input (not script) files cannot be
492opened for reading,
493.Nm sed
494continues to process the remaining
495files.
496.Sh STANDARDS
497The
498.Nm sed
499utility is expected to be
500.St -p1003.2
501compatible.