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