xref: /freebsd/usr.bin/sed/sed.1 (revision 941e286383714ef25f1ffe9ba6ae5040afdd7060)
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 May 24, 2009
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. The closing delimiter can also optionally be followed by the
195.Dq I
196character, to indicate that the regular expression is to be matched
197in a case-insensitive way.
198.El
199.Pp
200A command line with no addresses selects every pattern space.
201.Pp
202A command line with one address selects all of the pattern spaces
203that match the address.
204.Pp
205A command line with two addresses selects an inclusive range.
206This
207range starts with the first pattern space that matches the first
208address.
209The end of the range is the next following pattern space
210that matches the second address.
211If the second address is a number
212less than or equal to the line number first selected, only that
213line is selected.
214The number in the second address may be prefixed with a
215.Pq Dq \&+
216to specify the number of lines to match after the first pattern.
217In the case when the second address is a context
218address,
219.Nm
220does not re-match the second address against the
221pattern space that matched the first address.
222Starting at the
223first line following the selected range,
224.Nm
225starts looking again for the first address.
226.Pp
227Editing commands can be applied to non-selected pattern spaces by use
228of the exclamation character
229.Pq Dq \&!
230function.
231.Sh "Sed Regular Expressions"
232The regular expressions used in
233.Nm ,
234by default, are basic regular expressions (BREs, see
235.Xr re_format 7
236for more information), but extended (modern) regular expressions can be used
237instead if the
238.Fl E
239flag is given.
240In addition,
241.Nm
242has the following two additions to regular expressions:
243.Pp
244.Bl -enum -compact
245.It
246In a context address, any character other than a backslash
247.Pq Dq \e
248or newline character may be used to delimit the regular expression.
249The opening delimiter needs to be preceded by a backslash
250unless it is a slash.
251For example, the context address
252.Li \exabcx
253is equivalent to
254.Li /abc/ .
255Also, putting a backslash character before the delimiting character
256within the regular expression causes the character to be treated literally.
257For example, in the context address
258.Li \exabc\exdefx ,
259the RE delimiter is an
260.Dq x
261and the second
262.Dq x
263stands for itself, so that the regular expression is
264.Dq abcxdef .
265.Pp
266.It
267The escape sequence \en matches a newline character embedded in the
268pattern space.
269You cannot, however, use a literal newline character in an address or
270in the substitute command.
271.El
272.Pp
273One special feature of
274.Nm
275regular expressions is that they can default to the last regular
276expression used.
277If a regular expression is empty, i.e., just the delimiter characters
278are specified, the last regular expression encountered is used instead.
279The last regular expression is defined as the last regular expression
280used as part of an address or substitute command, and at run-time, not
281compile-time.
282For example, the command
283.Dq /abc/s//XXX/
284will substitute
285.Dq XXX
286for the pattern
287.Dq abc .
288.Sh "Sed Functions"
289In the following list of commands, the maximum number of permissible
290addresses for each command is indicated by [0addr], [1addr], or [2addr],
291representing zero, one, or two addresses.
292.Pp
293The argument
294.Em text
295consists of one or more lines.
296To embed a newline in the text, precede it with a backslash.
297Other backslashes in text are deleted and the following character
298taken literally.
299.Pp
300The
301.Dq r
302and
303.Dq w
304functions take an optional file parameter, which should be separated
305from the function letter by white space.
306Each file given as an argument to
307.Nm
308is created (or its contents truncated) before any input processing begins.
309.Pp
310The
311.Dq b ,
312.Dq r ,
313.Dq s ,
314.Dq t ,
315.Dq w ,
316.Dq y ,
317.Dq \&! ,
318and
319.Dq \&:
320functions all accept additional arguments.
321The following synopses indicate which arguments have to be separated from
322the function letters by white space characters.
323.Pp
324Two of the functions take a function-list.
325This is a list of
326.Nm
327functions separated by newlines, as follows:
328.Bd -literal -offset indent
329{ function
330  function
331  ...
332  function
333}
334.Ed
335.Pp
336The
337.Dq {
338can be preceded by white space and can be followed by white space.
339The function can be preceded by white space.
340The terminating
341.Dq }
342must be preceded by a newline or optional white space.
343.Pp
344.Bl -tag -width "XXXXXX" -compact
345.It [2addr] function-list
346Execute function-list only when the pattern space is selected.
347.Pp
348.It [1addr]a\e
349.It text
350Write
351.Em text
352to standard output immediately before each attempt to read a line of input,
353whether by executing the
354.Dq N
355function or by beginning a new cycle.
356.Pp
357.It [2addr]b[label]
358Branch to the
359.Dq \&:
360function with the specified label.
361If the label is not specified, branch to the end of the script.
362.Pp
363.It [2addr]c\e
364.It text
365Delete the pattern space.
366With 0 or 1 address or at the end of a 2-address range,
367.Em text
368is written to the standard output.
369.Pp
370.It [2addr]d
371Delete the pattern space and start the next cycle.
372.Pp
373.It [2addr]D
374Delete the initial segment of the pattern space through the first
375newline character and start the next cycle.
376.Pp
377.It [2addr]g
378Replace the contents of the pattern space with the contents of the
379hold space.
380.Pp
381.It [2addr]G
382Append a newline character followed by the contents of the hold space
383to the pattern space.
384.Pp
385.It [2addr]h
386Replace the contents of the hold space with the contents of the
387pattern space.
388.Pp
389.It [2addr]H
390Append a newline character followed by the contents of the pattern space
391to the hold space.
392.Pp
393.It [1addr]i\e
394.It text
395Write
396.Em text
397to the standard output.
398.Pp
399.It [2addr]l
400(The letter ell.)
401Write the pattern space to the standard output in a visually unambiguous
402form.
403This form is as follows:
404.Pp
405.Bl -tag -width "carriage-returnXX" -offset indent -compact
406.It backslash
407\e\e
408.It alert
409\ea
410.It form-feed
411\ef
412.It carriage-return
413\er
414.It tab
415\et
416.It vertical tab
417\ev
418.El
419.Pp
420Nonprintable characters are written as three-digit octal numbers (with a
421preceding backslash) for each byte in the character (most significant byte
422first).
423Long lines are folded, with the point of folding indicated by displaying
424a backslash followed by a newline.
425The end of each line is marked with a
426.Dq $ .
427.Pp
428.It [2addr]n
429Write the pattern space to the standard output if the default output has
430not been suppressed, and replace the pattern space with the next line of
431input.
432.Pp
433.It [2addr]N
434Append the next line of input to the pattern space, using an embedded
435newline character to separate the appended material from the original
436contents.
437Note that the current line number changes.
438.Pp
439.It [2addr]p
440Write the pattern space to standard output.
441.Pp
442.It [2addr]P
443Write the pattern space, up to the first newline character to the
444standard output.
445.Pp
446.It [1addr]q
447Branch to the end of the script and quit without starting a new cycle.
448.Pp
449.It [1addr]r file
450Copy the contents of
451.Em file
452to the standard output immediately before the next attempt to read a
453line of input.
454If
455.Em file
456cannot be read for any reason, it is silently ignored and no error
457condition is set.
458.Pp
459.It [2addr]s/regular expression/replacement/flags
460Substitute the replacement string for the first instance of the regular
461expression in the pattern space.
462Any character other than backslash or newline can be used instead of
463a slash to delimit the RE and the replacement.
464Within the RE and the replacement, the RE delimiter itself can be used as
465a literal character if it is preceded by a backslash.
466.Pp
467An ampersand
468.Pq Dq &
469appearing in the replacement is replaced by the string matching the RE.
470The special meaning of
471.Dq &
472in this context can be suppressed by preceding it by a backslash.
473The string
474.Dq \e# ,
475where
476.Dq #
477is a digit, is replaced by the text matched
478by the corresponding backreference expression (see
479.Xr re_format 7 ) .
480.Pp
481A line can be split by substituting a newline character into it.
482To specify a newline character in the replacement string, precede it with
483a backslash.
484.Pp
485The value of
486.Em flags
487in the substitute function is zero or more of the following:
488.Bl -tag -width "XXXXXX" -offset indent
489.It Ar N
490Make the substitution only for the
491.Ar N Ns 'th
492occurrence of the regular expression in the pattern space.
493.It g
494Make the substitution for all non-overlapping matches of the
495regular expression, not just the first one.
496.It p
497Write the pattern space to standard output if a replacement was made.
498If the replacement string is identical to that which it replaces, it
499is still considered to have been a replacement.
500.It w Em file
501Append the pattern space to
502.Em file
503if a replacement was made.
504If the replacement string is identical to that which it replaces, it
505is still considered to have been a replacement.
506.It I
507Match the regular expression in a case-insensitive way.
508.El
509.Pp
510.It [2addr]t [label]
511Branch to the
512.Dq \&:
513function bearing the label if any substitutions have been made since the
514most recent reading of an input line or execution of a
515.Dq t
516function.
517If no label is specified, branch to the end of the script.
518.Pp
519.It [2addr]w Em file
520Append the pattern space to the
521.Em file .
522.Pp
523.It [2addr]x
524Swap the contents of the pattern and hold spaces.
525.Pp
526.It [2addr]y/string1/string2/
527Replace all occurrences of characters in
528.Em string1
529in the pattern space with the corresponding characters from
530.Em string2 .
531Any character other than a backslash or newline can be used instead of
532a slash to delimit the strings.
533Within
534.Em string1
535and
536.Em string2 ,
537a backslash followed by any character other than a newline is that literal
538character, and a backslash followed by an ``n'' is replaced by a newline
539character.
540.Pp
541.It [2addr]!function
542.It [2addr]!function-list
543Apply the function or function-list only to the lines that are
544.Em not
545selected by the address(es).
546.Pp
547.It [0addr]:label
548This function does nothing; it bears a label to which the
549.Dq b
550and
551.Dq t
552commands may branch.
553.Pp
554.It [1addr]=
555Write the line number to the standard output followed by a newline
556character.
557.Pp
558.It [0addr]
559Empty lines are ignored.
560.Pp
561.It [0addr]#
562The
563.Dq #
564and the remainder of the line are ignored (treated as a comment), with
565the single exception that if the first two characters in the file are
566.Dq #n ,
567the default output is suppressed.
568This is the same as specifying the
569.Fl n
570option on the command line.
571.El
572.Sh ENVIRONMENT
573The
574.Ev COLUMNS , LANG , LC_ALL , LC_CTYPE
575and
576.Ev LC_COLLATE
577environment variables affect the execution of
578.Nm
579as described in
580.Xr environ 7 .
581.Sh EXIT STATUS
582.Ex -std
583.Sh SEE ALSO
584.Xr awk 1 ,
585.Xr ed 1 ,
586.Xr grep 1 ,
587.Xr regex 3 ,
588.Xr re_format 7
589.Sh STANDARDS
590The
591.Nm
592utility is expected to be a superset of the
593.St -p1003.2
594specification.
595.Pp
596The
597.Fl E , I , a
598and
599.Fl i
600options, the prefixing
601.Dq \&+
602in the second member of an address range,
603as well as the
604.Dq I
605flag to the address regular expression and substitution command are
606non-standard
607.Fx
608extensions and may not be available on other operating systems.
609.Sh HISTORY
610A
611.Nm
612command, written by
613.An L. E. McMahon ,
614appeared in
615.At v7 .
616.Sh AUTHORS
617.An "Diomidis D. Spinellis" Aq dds@FreeBSD.org
618.Sh BUGS
619Multibyte characters containing a byte with value 0x5C
620.Tn ( ASCII
621.Ql \e )
622may be incorrectly treated as line continuation characters in arguments to the
623.Dq a ,
624.Dq c
625and
626.Dq i
627commands.
628Multibyte characters cannot be used as delimiters with the
629.Dq s
630and
631.Dq y
632commands.
633