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