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