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