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