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