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