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