xref: /freebsd/usr.bin/sed/sed.1 (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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.Po
138.Dq $
139.Pc
140character that addresses the last line of input, or a context address
141(which consists of a regular expression preceded and followed by a
142delimiter).
143.Pp
144A command line with no addresses selects every pattern space.
145.Pp
146A command line with one address selects all of the pattern spaces
147that match the address.
148.Pp
149A command line with two addresses selects an inclusive range.  This
150range starts with the first pattern space that matches the first
151address.  The end of the range is the next following pattern space
152that matches the second address.  If the second address is a number
153less than or equal to the line number first selected, only that
154line is selected.  In the case when the second address is a context
155address, sed does not re-match the second address against the
156pattern space that matched the first address.  Starting at the
157first line following the selected range, sed starts looking again
158for the first address.
159.Nm
160starts looking again for the first address.
161.Pp
162Editing commands can be applied to non-selected pattern spaces by use
163of the exclamation character
164.Po
165.Dq \&!
166.Pc
167function.
168.Sh "Sed Regular Expressions"
169The regular expressions used in
170.Nm ,
171by default, are basic regular expressions (BREs, see
172.Xr re_format 7
173for more information).
174.Nm
175can use extended (modern) regular expressions instead if the
176.Fl E
177flag is given.
178In addition,
179.Nm
180has the following two additions to regular expressions:
181.Pp
182.Bl -enum -compact
183.It
184In a context address, any character other than a backslash
185.Po
186.Dq \e
187.Pc
188or newline character may be used to delimit the regular expression.
189Also, putting a backslash character before the delimiting character
190causes the character to be treated literally.
191For example, in the context address \exabc\exdefx, the RE delimiter
192is an
193.Dq x
194and the second
195.Dq x
196stands for itself, so that the regular expression is
197.Dq abcxdef .
198.Pp
199.It
200The escape sequence \en matches a newline character embedded in the
201pattern space.
202You can't, however, use a literal newline character in an address or
203in the substitute command.
204.El
205.Pp
206One special feature of
207.Nm
208regular expressions is that they can default to the last regular
209expression used.
210If a regular expression is empty, i.e. just the delimiter characters
211are specified, the last regular expression encountered is used instead.
212The last regular expression is defined as the last regular expression
213used as part of an address or substitute command, and at run-time, not
214compile-time.
215For example, the command
216.Dq /abc/s//XXX/
217will substitute
218.Dq XXX
219for the pattern
220.Dq abc .
221.Sh "Sed Functions"
222In the following list of commands, the maximum number of permissible
223addresses for each command is indicated by [0addr], [1addr], or [2addr],
224representing zero, one, or two addresses.
225.Pp
226The argument
227.Em text
228consists of one or more lines.
229To embed a newline in the text, precede it with a backslash.
230Other backslashes in text are deleted and the following character
231taken literally.
232.Pp
233The
234.Dq r
235and
236.Dq w
237functions take an optional file parameter, which should be separated
238from the function letter by white space.
239Each file given as an argument to
240.Nm
241is created (or its contents truncated) before any input processing begins.
242.Pp
243The
244.Dq b ,
245.Dq r ,
246.Dq s ,
247.Dq t ,
248.Dq w ,
249.Dq y ,
250.Dq \&! ,
251and
252.Dq \&:
253functions all accept additional arguments.
254The following synopses indicate which arguments have to be separated from
255the function letters by white space characters.
256.Pp
257Two of the functions take a function-list.
258This is a list of
259.Nm
260functions separated by newlines, as follows:
261.Bd -literal -offset indent
262{ function
263  function
264  ...
265  function
266}
267.Ed
268.Pp
269The
270.Dq {
271can be preceded by white space and can be followed by white space.
272The function can be preceded by white space.
273The terminating
274.Dq }
275must be preceded by a newline or optional white space.
276.Pp
277.Bl -tag -width "XXXXXX" -compact
278.It [2addr] function-list
279Execute function-list only when the pattern space is selected.
280.Pp
281.It [1addr]a\e
282.It text
283Write
284.Em text
285to standard output immediately before each attempt to read a line of input,
286whether by executing the
287.Dq N
288function or by beginning a new cycle.
289.Pp
290.It [2addr]b[label]
291Branch to the
292.Dq \&:
293function with the specified label.
294If the label is not specified, branch to the end of the script.
295.Pp
296.It [2addr]c\e
297.It text
298Delete the pattern space.
299With 0 or 1 address or at the end of a 2-address range,
300.Em text
301is written to the standard output.
302.Pp
303.It [2addr]d
304Delete the pattern space and start the next cycle.
305.Pp
306.It [2addr]D
307Delete the initial segment of the pattern space through the first
308newline character and start the next cycle.
309.Pp
310.It [2addr]g
311Replace the contents of the pattern space with the contents of the
312hold space.
313.Pp
314.It [2addr]G
315Append a newline character followed by the contents of the hold space
316to the pattern space.
317.Pp
318.It [2addr]h
319Replace the contents of the hold space with the contents of the
320pattern space.
321.Pp
322.It [2addr]H
323Append a newline character followed by the contents of the pattern space
324to the hold space.
325.Pp
326.It [1addr]i\e
327.It text
328Write
329.Em text
330to the standard output.
331.Pp
332.It [2addr]l
333(The letter ell.)
334Write the pattern space to the standard output in a visually unambiguous
335form.
336This form is as follows:
337.Pp
338.Bl -tag -width "carriage-returnXX" -offset indent -compact
339.It backslash
340\e\e
341.It alert
342\ea
343.It form-feed
344\ef
345.It newline
346\en
347.It carriage-return
348\er
349.It tab
350\et
351.It vertical tab
352\ev
353.El
354.Pp
355Nonprintable characters are written as three-digit octal numbers (with a
356preceding backslash) for each byte in the character (most significant byte
357first).
358Long lines are folded, with the point of folding indicated by displaying
359a backslash followed by a newline.
360The end of each line is marked with a
361.Dq $ .
362.Pp
363.It [2addr]n
364Write the pattern space to the standard output if the default output has
365not been suppressed, and replace the pattern space with the next line of
366input.
367.Pp
368.It [2addr]N
369Append the next line of input to the pattern space, using an embedded
370newline character to separate the appended material from the original
371contents.
372Note that the current line number changes.
373.Pp
374.It [2addr]p
375Write the pattern space to standard output.
376.Pp
377.It [2addr]P
378Write the pattern space, up to the first newline character to the
379standard output.
380.Pp
381.It [1addr]q
382Branch to the end of the script and quit without starting a new cycle.
383.Pp
384.It [1addr]r file
385Copy the contents of
386.Em file
387to the standard output immediately before the next attempt to read a
388line of input.
389If
390.Em file
391cannot be read for any reason, it is silently ignored and no error
392condition is set.
393.Pp
394.It [2addr]s/regular expression/replacement/flags
395Substitute the replacement string for the first instance of the regular
396expression in the pattern space.
397Any character other than backslash or newline can be used instead of
398a slash to delimit the RE and the replacement.
399Within the RE and the replacement, the RE delimiter itself can be used as
400a literal character if it is preceded by a backslash.
401.Pp
402An ampersand
403.Po
404.Dq &
405.Pc
406appearing in the replacement is replaced by the string matching the RE.
407The special meaning of
408.Dq &
409in this context can be suppressed by preceding it by a backslash.
410The string
411.Dq \e# ,
412where
413.Dq #
414is a digit, is replaced by the text matched
415by the corresponding backreference expression (see
416.Xr re_format 7 ) .
417.Pp
418A line can be split by substituting a newline character into it.
419To specify a newline character in the replacement string, precede it with
420a backslash.
421.Pp
422The value of
423.Em flags
424in the substitute function is zero or more of the following:
425.Bl -tag -width "XXXXXX" -offset indent
426.It "0 ... 9"
427Make the substitution only for the N'th occurrence of the regular
428expression in the pattern space.
429.It g
430Make the substitution for all non-overlapping matches of the
431regular expression, not just the first one.
432.It p
433Write the pattern space to standard output if a replacement was made.
434If the replacement string is identical to that which it replaces, it
435is still considered to have been a replacement.
436.It w Em file
437Append the pattern space to
438.Em file
439if a replacement was made.
440If the replacement string is identical to that which it replaces, it
441is still considered to have been a replacement.
442.El
443.Pp
444.It [2addr]t [label]
445Branch to the
446.Dq \&:
447function bearing the label if any substitutions have been made since the
448most recent reading of an input line or execution of a
449.Dq t
450function.
451If no label is specified, branch to the end of the script.
452.Pp
453.It [2addr]w Em file
454Append the pattern space to the
455.Em file .
456.Pp
457.It [2addr]x
458Swap the contents of the pattern and hold spaces.
459.Pp
460.It [2addr]y/string1/string2/
461Replace all occurrences of characters in
462.Em string1
463in the pattern space with the corresponding characters from
464.Em string2 .
465Any character other than a backslash or newline can be used instead of
466a slash to delimit the strings.
467Within
468.Em string1
469and
470.Em string2 ,
471a backslash followed by any character other than a newline is that literal
472character, and a backslash followed by an ``n'' is replaced by a newline
473character.
474.Pp
475.It [2addr]!function
476.It [2addr]!function-list
477Apply the function or function-list only to the lines that are
478.Em not
479selected by the address(es).
480.Pp
481.It [0addr]:label
482This function does nothing; it bears a label to which the
483.Dq b
484and
485.Dq t
486commands may branch.
487.Pp
488.It [1addr]=
489Write the line number to the standard output followed by a newline
490character.
491.Pp
492.It [0addr]
493Empty lines are ignored.
494.Pp
495.It [0addr]#
496The
497.Dq #
498and the remainder of the line are ignored (treated as a comment), with
499the single exception that if the first two characters in the file are
500.Dq #n ,
501the default output is suppressed.
502This is the same as specifying the
503.Fl n
504option on the command line.
505.El
506.Sh DIAGNOSTICS
507The
508.Nm
509utility exits 0 on success, and >0 if an error occurs.
510.Sh SEE ALSO
511.Xr awk 1 ,
512.Xr ed 1 ,
513.Xr grep 1 ,
514.Xr regex 3 ,
515.Xr re_format 7
516.Sh HISTORY
517A
518.Nm
519command appeared in
520.At v7 .
521.Sh STANDARDS
522The
523.Nm
524function is expected to be a superset of the
525.St -p1003.2
526specification.
527