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