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