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