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