xref: /freebsd/usr.bin/sed/sed.1 (revision ee2ea5ceafed78a5bd9810beb9e3ca927180c226)
1.\" Copyright (c) 1992, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"	@(#)sed.1	8.2 (Berkeley) 12/30/93
36.\" $FreeBSD$
37.\"
38.Dd December 30, 1993
39.Dt SED 1
40.Os
41.Sh NAME
42.Nm sed
43.Nd stream editor
44.Sh SYNOPSIS
45.Nm
46.Op Fl Ean
47.Ar command
48.Op Ar
49.Nm
50.Op Fl Ean
51.Op Fl e Ar command
52.Op Fl f Ar command_file
53.Op Ar
54.Sh DESCRIPTION
55The
56.Nm
57utility reads the specified files, or the standard input if no files
58are specified, modifying the input as specified by a list of commands.
59The input is then written to the standard output.
60.Pp
61A single command may be specified as the first argument to
62.Nm .
63Multiple commands may be specified by using the
64.Fl e
65or
66.Fl f
67options.
68All commands are applied to the input in the order they are specified
69regardless of their origin.
70.Pp
71The following options are available:
72.Bl -tag -width indent
73.It Fl E
74Interpret regular expressions as extended (modern) regular expressions
75rather than basic regular expressions (BRE's).
76The
77.Xr re_format 7
78manual page fully describes both formats.
79.It Fl a
80The files listed as parameters for the
81.Dq w
82functions are created (or truncated) before any processing begins,
83by default.
84The
85.Fl a
86option causes
87.Nm
88to delay opening each file until a command containing the related
89.Dq w
90function is applied to a line of input.
91.It Fl e Ar command
92Append the editing commands specified by the
93.Ar command
94argument
95to the list of commands.
96.It Fl f Ar command_file
97Append the editing commands found in the file
98.Ar command_file
99to the list of commands.
100The editing commands should each be listed on a separate line.
101.It Fl n
102By default, each line of input is echoed to the standard output after
103all of the commands have been applied to it.
104The
105.Fl n
106option suppresses this behavior.
107.El
108.Pp
109The form of a
110.Nm
111command is as follows:
112.Pp
113.Dl [address[,address]]function[arguments]
114.Pp
115Whitespace may be inserted before the first address and the function
116portions of the command.
117.Pp
118Normally,
119.Nm
120cyclically copies a line of input, not including its terminating newline
121character, into a
122.Em "pattern space" ,
123(unless there is something left after a
124.Dq D
125function),
126applies all of the commands with addresses that select that pattern space,
127copies the pattern space to the standard output, appending a newline, and
128deletes the pattern space.
129.Pp
130Some of the functions use a
131.Em "hold space"
132to save all or part of the pattern space for subsequent retrieval.
133.Sh "Sed Addresses"
134An address is not required, but if specified must be a number (that counts
135input lines
136cumulatively across input files), a dollar
137.Pq Dq $
138character that addresses the last line of input, or a context address
139(which consists of a regular expression preceded and followed by a
140delimiter).
141.Pp
142A command line with no addresses selects every pattern space.
143.Pp
144A command line with one address selects all of the pattern spaces
145that match the address.
146.Pp
147A command line with two addresses selects an inclusive range.  This
148range starts with the first pattern space that matches the first
149address.  The end of the range is the next following pattern space
150that matches the second address.  If the second address is a number
151less than or equal to the line number first selected, only that
152line is selected.  In the case when the second address is a context
153address, sed does not re-match the second address against the
154pattern space that matched the first address.  Starting at the
155first line following the selected range,
156.Nm
157starts looking again for the first address.
158.Pp
159Editing commands can be applied to non-selected pattern spaces by use
160of the exclamation character
161.Pq Dq \&!
162function.
163.Sh "Sed Regular Expressions"
164The regular expressions used in
165.Nm ,
166by default, are basic regular expressions (BREs, see
167.Xr re_format 7
168for more information).
169.Nm
170can use extended (modern) regular expressions instead if the
171.Fl E
172flag is given.
173In addition,
174.Nm
175has the following two additions to regular expressions:
176.Pp
177.Bl -enum -compact
178.It
179In a context address, any character other than a backslash
180.Pq Dq \e
181or newline character may be used to delimit the regular expression.
182Also, putting a backslash character before the delimiting character
183causes the character to be treated literally.
184For example, in the context address \exabc\exdefx, the RE delimiter
185is an
186.Dq x
187and the second
188.Dq x
189stands for itself, so that the regular expression is
190.Dq abcxdef .
191.Pp
192.It
193The escape sequence \en matches a newline character embedded in the
194pattern space.
195You can't, however, use a literal newline character in an address or
196in the substitute command.
197.El
198.Pp
199One special feature of
200.Nm
201regular expressions is that they can default to the last regular
202expression used.
203If a regular expression is empty, i.e. just the delimiter characters
204are specified, the last regular expression encountered is used instead.
205The last regular expression is defined as the last regular expression
206used as part of an address or substitute command, and at run-time, not
207compile-time.
208For example, the command
209.Dq /abc/s//XXX/
210will substitute
211.Dq XXX
212for the pattern
213.Dq abc .
214.Sh "Sed Functions"
215In the following list of commands, the maximum number of permissible
216addresses for each command is indicated by [0addr], [1addr], or [2addr],
217representing zero, one, or two addresses.
218.Pp
219The argument
220.Em text
221consists of one or more lines.
222To embed a newline in the text, precede it with a backslash.
223Other backslashes in text are deleted and the following character
224taken literally.
225.Pp
226The
227.Dq r
228and
229.Dq w
230functions take an optional file parameter, which should be separated
231from the function letter by white space.
232Each file given as an argument to
233.Nm
234is created (or its contents truncated) before any input processing begins.
235.Pp
236The
237.Dq b ,
238.Dq r ,
239.Dq s ,
240.Dq t ,
241.Dq w ,
242.Dq y ,
243.Dq \&! ,
244and
245.Dq \&:
246functions all accept additional arguments.
247The following synopses indicate which arguments have to be separated from
248the function letters by white space characters.
249.Pp
250Two of the functions take a function-list.
251This is a list of
252.Nm
253functions separated by newlines, as follows:
254.Bd -literal -offset indent
255{ function
256  function
257  ...
258  function
259}
260.Ed
261.Pp
262The
263.Dq {
264can be preceded by white space and can be followed by white space.
265The function can be preceded by white space.
266The terminating
267.Dq }
268must be preceded by a newline or optional white space.
269.Pp
270.Bl -tag -width "XXXXXX" -compact
271.It [2addr] function-list
272Execute function-list only when the pattern space is selected.
273.Pp
274.It [1addr]a\e
275.It text
276Write
277.Em text
278to standard output immediately before each attempt to read a line of input,
279whether by executing the
280.Dq N
281function or by beginning a new cycle.
282.Pp
283.It [2addr]b[label]
284Branch to the
285.Dq \&:
286function with the specified label.
287If the label is not specified, branch to the end of the script.
288.Pp
289.It [2addr]c\e
290.It text
291Delete the pattern space.
292With 0 or 1 address or at the end of a 2-address range,
293.Em text
294is written to the standard output.
295.Pp
296.It [2addr]d
297Delete the pattern space and start the next cycle.
298.Pp
299.It [2addr]D
300Delete the initial segment of the pattern space through the first
301newline character and start the next cycle.
302.Pp
303.It [2addr]g
304Replace the contents of the pattern space with the contents of the
305hold space.
306.Pp
307.It [2addr]G
308Append a newline character followed by the contents of the hold space
309to the pattern space.
310.Pp
311.It [2addr]h
312Replace the contents of the hold space with the contents of the
313pattern space.
314.Pp
315.It [2addr]H
316Append a newline character followed by the contents of the pattern space
317to the hold space.
318.Pp
319.It [1addr]i\e
320.It text
321Write
322.Em text
323to the standard output.
324.Pp
325.It [2addr]l
326(The letter ell.)
327Write the pattern space to the standard output in a visually unambiguous
328form.
329This form is as follows:
330.Pp
331.Bl -tag -width "carriage-returnXX" -offset indent -compact
332.It backslash
333\e\e
334.It alert
335\ea
336.It form-feed
337\ef
338.It newline
339\en
340.It carriage-return
341\er
342.It tab
343\et
344.It vertical tab
345\ev
346.El
347.Pp
348Nonprintable characters are written as three-digit octal numbers (with a
349preceding backslash) for each byte in the character (most significant byte
350first).
351Long lines are folded, with the point of folding indicated by displaying
352a backslash followed by a newline.
353The end of each line is marked with a
354.Dq $ .
355.Pp
356.It [2addr]n
357Write the pattern space to the standard output if the default output has
358not been suppressed, and replace the pattern space with the next line of
359input.
360.Pp
361.It [2addr]N
362Append the next line of input to the pattern space, using an embedded
363newline character to separate the appended material from the original
364contents.
365Note that the current line number changes.
366.Pp
367.It [2addr]p
368Write the pattern space to standard output.
369.Pp
370.It [2addr]P
371Write the pattern space, up to the first newline character to the
372standard output.
373.Pp
374.It [1addr]q
375Branch to the end of the script and quit without starting a new cycle.
376.Pp
377.It [1addr]r file
378Copy the contents of
379.Em file
380to the standard output immediately before the next attempt to read a
381line of input.
382If
383.Em file
384cannot be read for any reason, it is silently ignored and no error
385condition is set.
386.Pp
387.It [2addr]s/regular expression/replacement/flags
388Substitute the replacement string for the first instance of the regular
389expression in the pattern space.
390Any character other than backslash or newline can be used instead of
391a slash to delimit the RE and the replacement.
392Within the RE and the replacement, the RE delimiter itself can be used as
393a literal character if it is preceded by a backslash.
394.Pp
395An ampersand
396.Pq Dq &
397appearing in the replacement is replaced by the string matching the RE.
398The special meaning of
399.Dq &
400in this context can be suppressed by preceding it by a backslash.
401The string
402.Dq \e# ,
403where
404.Dq #
405is a digit, is replaced by the text matched
406by the corresponding backreference expression (see
407.Xr re_format 7 ) .
408.Pp
409A line can be split by substituting a newline character into it.
410To specify a newline character in the replacement string, precede it with
411a backslash.
412.Pp
413The value of
414.Em flags
415in the substitute function is zero or more of the following:
416.Bl -tag -width "XXXXXX" -offset indent
417.It "0 ... 9"
418Make the substitution only for the N'th occurrence of the regular
419expression in the pattern space.
420.It g
421Make the substitution for all non-overlapping matches of the
422regular expression, not just the first one.
423.It p
424Write the pattern space to standard output if a replacement was made.
425If the replacement string is identical to that which it replaces, it
426is still considered to have been a replacement.
427.It w Em file
428Append the pattern space to
429.Em file
430if a replacement was made.
431If the replacement string is identical to that which it replaces, it
432is still considered to have been a replacement.
433.El
434.Pp
435.It [2addr]t [label]
436Branch to the
437.Dq \&:
438function bearing the label if any substitutions have been made since the
439most recent reading of an input line or execution of a
440.Dq t
441function.
442If no label is specified, branch to the end of the script.
443.Pp
444.It [2addr]w Em file
445Append the pattern space to the
446.Em file .
447.Pp
448.It [2addr]x
449Swap the contents of the pattern and hold spaces.
450.Pp
451.It [2addr]y/string1/string2/
452Replace all occurrences of characters in
453.Em string1
454in the pattern space with the corresponding characters from
455.Em string2 .
456Any character other than a backslash or newline can be used instead of
457a slash to delimit the strings.
458Within
459.Em string1
460and
461.Em string2 ,
462a backslash followed by any character other than a newline is that literal
463character, and a backslash followed by an ``n'' is replaced by a newline
464character.
465.Pp
466.It [2addr]!function
467.It [2addr]!function-list
468Apply the function or function-list only to the lines that are
469.Em not
470selected by the address(es).
471.Pp
472.It [0addr]:label
473This function does nothing; it bears a label to which the
474.Dq b
475and
476.Dq t
477commands may branch.
478.Pp
479.It [1addr]=
480Write the line number to the standard output followed by a newline
481character.
482.Pp
483.It [0addr]
484Empty lines are ignored.
485.Pp
486.It [0addr]#
487The
488.Dq #
489and the remainder of the line are ignored (treated as a comment), with
490the single exception that if the first two characters in the file are
491.Dq #n ,
492the default output is suppressed.
493This is the same as specifying the
494.Fl n
495option on the command line.
496.El
497.Sh DIAGNOSTICS
498.Ex -std
499.Sh SEE ALSO
500.Xr awk 1 ,
501.Xr ed 1 ,
502.Xr grep 1 ,
503.Xr regex 3 ,
504.Xr re_format 7
505.Sh HISTORY
506A
507.Nm
508command appeared in
509.At v7 .
510.Sh STANDARDS
511The
512.Nm
513function is expected to be a superset of the
514.St -p1003.2
515specification.
516