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