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