MFC:
[dragonfly.git] / usr.bin / sed / sed.1
blob6be27e1a4615a29a4a30e4132fb9438c63567b5c
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
54 The
55 .Nm
56 utility reads the specified files, or the standard input if no files
57 are specified, modifying the input as specified by a list of commands.
58 The input is then written to the standard output.
59 .Pp
60 A single command may be specified as the first argument to
61 .Nm .
62 Multiple commands may be specified by using the
63 .Fl e
65 .Fl f
66 options.
67 All commands are applied to the input in the order they are specified
68 regardless of their origin.
69 .Pp
70 The following options are available:
71 .Bl -tag -width indent
72 .It Fl E
73 Interpret regular expressions as extended (modern) regular expressions
74 rather than basic regular expressions (BRE's).
75 The
76 .Xr re_format 7
77 manual page fully describes both formats.
78 .It Fl a
79 The files listed as parameters for the
80 .Dq w
81 functions are created (or truncated) before any processing begins,
82 by default.
83 The
84 .Fl a
85 option causes
86 .Nm
87 to delay opening each file until a command containing the related
88 .Dq w
89 function is applied to a line of input.
90 .It Fl e Ar command
91 Append the editing commands specified by the
92 .Ar command
93 argument
94 to the list of commands.
95 .It Fl f Ar command_file
96 Append the editing commands found in the file
97 .Ar command_file
98 to the list of commands.
99 The editing commands should each be listed on a separate line.
100 .It Fl I Ar extension
101 Edit files in-place, saving backups with the specified
102 .Ar extension .
103 If a zero-length
104 .Ar extension
105 is given, no backup will be saved.
106 It is not recommended to give a zero-length
107 .Ar extension
108 when in-place editing files, as you risk corruption or partial content
109 in situations where disk space is exhausted, etc.
111 Note that in-place editing with
112 .Fl I
113 still takes place in a single continuous line address space covering
114 all files, although each file preserves its individuality instead of
115 forming one output stream.
116 The line counter is never reset between files, address ranges can span
117 file boundaries, and the
118 .Dq $
119 address matches only the last line of the last file.
120 (See
121 .Sx "Sed Addresses" . )
122 That can lead to unexpected results in many cases of in-place editing,
123 where using
124 .Fl i
125 is desired.
126 .It Fl i Ar extension
127 Edit files in-place similarly to
128 .Fl I ,
129 but treat each file independently from other files.
130 In particular, line numbers in each file start at 1,
132 .Dq $
133 address matches the last line of the current file,
134 and address ranges are limited to the current file.
135 (See
136 .Sx "Sed Addresses" . )
137 The net result is as though each file were edited by a separate
139 instance.
140 .It Fl l
141 Make output line buffered.
142 .It Fl n
143 By default, each line of input is echoed to the standard output after
144 all of the commands have been applied to it.
146 .Fl n
147 option suppresses this behavior.
150 The form of a
152 command is as follows:
154 .Dl [address[,address]]function[arguments]
156 Whitespace may be inserted before the first address and the function
157 portions of the command.
159 Normally,
161 cyclically copies a line of input, not including its terminating newline
162 character, into a
163 .Em "pattern space" ,
164 (unless there is something left after a
165 .Dq D
166 function),
167 applies all of the commands with addresses that select that pattern space,
168 copies the pattern space to the standard output, appending a newline, and
169 deletes the pattern space.
171 Some of the functions use a
172 .Em "hold space"
173 to save all or part of the pattern space for subsequent retrieval.
174 .Sh "Sed Addresses"
175 An address is not required, but if specified must have one of the
176 following formats:
177 .Bl -bullet -offset indent
179 a number that counts
180 input lines
181 cumulatively across input files (or in each file independently
182 if a
183 .Fl i
184 option is in effect);
186 a dollar
187 .Pq Dq $
188 character that addresses the last line of input (or the last line
189 of the current file if a
190 .Fl i
191 option was specified);
193 a context address
194 that consists of a regular expression preceded and followed by a
195 delimiter.
196 The closing delimiter can also optionally be followed by the
197 .Dq I
198 character, to indicate that the regular expression is to be matched
199 in a case-insensitive way.
202 A command line with no addresses selects every pattern space.
204 A command line with one address selects all of the pattern spaces
205 that match the address.
207 A command line with two addresses selects an inclusive range.
208 This
209 range starts with the first pattern space that matches the first
210 address.
211 The end of the range is the next following pattern space
212 that matches the second address.
213 If the second address is a number
214 less than or equal to the line number first selected, only that
215 line is selected.
216 In the case when the second address is a context
217 address,
219 does not re-match the second address against the
220 pattern space that matched the first address.
221 Starting at the
222 first line following the selected range,
224 starts looking again for the first address.
226 Editing commands can be applied to non-selected pattern spaces by use
227 of the exclamation character
228 .Pq Dq \&!
229 function.
230 .Sh "Sed Regular Expressions"
231 The regular expressions used in
232 .Nm ,
233 by default, are basic regular expressions (BREs, see
234 .Xr re_format 7
235 for more information), but extended (modern) regular expressions can be used
236 instead if the
237 .Fl E
238 flag is given.
239 In addition,
241 has the following two additions to regular expressions:
243 .Bl -enum -compact
245 In a context address, any character other than a backslash
246 .Pq Dq \e
247 or newline character may be used to delimit the regular expression.
248 Also, putting a backslash character before the delimiting character
249 causes the character to be treated literally.
250 For example, in the context address \exabc\exdefx, the RE delimiter
251 is an
252 .Dq x
253 and the second
254 .Dq x
255 stands for itself, so that the regular expression is
256 .Dq abcxdef .
259 The escape sequence \en matches a newline character embedded in the
260 pattern space.
261 You cannot, however, use a literal newline character in an address or
262 in the substitute command.
265 One special feature of
267 regular expressions is that they can default to the last regular
268 expression used.
269 If a regular expression is empty, i.e., just the delimiter characters
270 are specified, the last regular expression encountered is used instead.
271 The last regular expression is defined as the last regular expression
272 used as part of an address or substitute command, and at run-time, not
273 compile-time.
274 For example, the command
275 .Dq /abc/s//XXX/
276 will substitute
277 .Dq XXX
278 for the pattern
279 .Dq abc .
280 .Sh "Sed Functions"
281 In the following list of commands, the maximum number of permissible
282 addresses for each command is indicated by [0addr], [1addr], or [2addr],
283 representing zero, one, or two addresses.
285 The argument
286 .Em text
287 consists of one or more lines.
288 To embed a newline in the text, precede it with a backslash.
289 Other backslashes in text are deleted and the following character
290 taken literally.
293 .Dq r
295 .Dq w
296 functions take an optional file parameter, which should be separated
297 from the function letter by white space.
298 Each file given as an argument to
300 is created (or its contents truncated) before any input processing begins.
303 .Dq b ,
304 .Dq r ,
305 .Dq s ,
306 .Dq t ,
307 .Dq w ,
308 .Dq y ,
309 .Dq \&! ,
311 .Dq \&:
312 functions all accept additional arguments.
313 The following synopses indicate which arguments have to be separated from
314 the function letters by white space characters.
316 Two of the functions take a function-list.
317 This is a list of
319 functions separated by newlines, as follows:
320 .Bd -literal -offset indent
321 { function
322   function
323   ...
324   function
329 .Dq {
330 can be preceded by white space and can be followed by white space.
331 The function can be preceded by white space.
332 The terminating
333 .Dq }
334 must be preceded by a newline or optional white space.
336 .Bl -tag -width "XXXXXX" -compact
337 .It [2addr] function-list
338 Execute function-list only when the pattern space is selected.
340 .It [1addr]a\e
341 .It text
342 Write
343 .Em text
344 to standard output immediately before each attempt to read a line of input,
345 whether by executing the
346 .Dq N
347 function or by beginning a new cycle.
349 .It [2addr]b[label]
350 Branch to the
351 .Dq \&:
352 function with the specified label.
353 If the label is not specified, branch to the end of the script.
355 .It [2addr]c\e
356 .It text
357 Delete the pattern space.
358 With 0 or 1 address or at the end of a 2-address range,
359 .Em text
360 is written to the standard output.
362 .It [2addr]d
363 Delete the pattern space and start the next cycle.
365 .It [2addr]D
366 Delete the initial segment of the pattern space through the first
367 newline character and start the next cycle.
369 .It [2addr]g
370 Replace the contents of the pattern space with the contents of the
371 hold space.
373 .It [2addr]G
374 Append a newline character followed by the contents of the hold space
375 to the pattern space.
377 .It [2addr]h
378 Replace the contents of the hold space with the contents of the
379 pattern space.
381 .It [2addr]H
382 Append a newline character followed by the contents of the pattern space
383 to the hold space.
385 .It [1addr]i\e
386 .It text
387 Write
388 .Em text
389 to the standard output.
391 .It [2addr]l
392 (The letter ell.)
393 Write the pattern space to the standard output in a visually unambiguous
394 form.
395 This form is as follows:
397 .Bl -tag -width "carriage-returnXX" -offset indent -compact
398 .It backslash
399 \e\e
400 .It alert
402 .It form-feed
404 .It carriage-return
406 .It tab
408 .It vertical tab
412 Nonprintable characters are written as three-digit octal numbers (with a
413 preceding backslash) for each byte in the character (most significant byte
414 first).
415 Long lines are folded, with the point of folding indicated by displaying
416 a backslash followed by a newline.
417 The end of each line is marked with a
418 .Dq $ .
420 .It [2addr]n
421 Write the pattern space to the standard output if the default output has
422 not been suppressed, and replace the pattern space with the next line of
423 input.
425 .It [2addr]N
426 Append the next line of input to the pattern space, using an embedded
427 newline character to separate the appended material from the original
428 contents.
429 Note that the current line number changes.
431 .It [2addr]p
432 Write the pattern space to standard output.
434 .It [2addr]P
435 Write the pattern space, up to the first newline character to the
436 standard output.
438 .It [1addr]q
439 Branch to the end of the script and quit without starting a new cycle.
441 .It [1addr]r file
442 Copy the contents of
443 .Em file
444 to the standard output immediately before the next attempt to read a
445 line of input.
447 .Em file
448 cannot be read for any reason, it is silently ignored and no error
449 condition is set.
451 .It [2addr]s/regular expression/replacement/flags
452 Substitute the replacement string for the first instance of the regular
453 expression in the pattern space.
454 Any character other than backslash or newline can be used instead of
455 a slash to delimit the RE and the replacement.
456 Within the RE and the replacement, the RE delimiter itself can be used as
457 a literal character if it is preceded by a backslash.
459 An ampersand
460 .Pq Dq &
461 appearing in the replacement is replaced by the string matching the RE.
462 The special meaning of
463 .Dq &
464 in this context can be suppressed by preceding it by a backslash.
465 The string
466 .Dq \e# ,
467 where
468 .Dq #
469 is a digit, is replaced by the text matched
470 by the corresponding backreference expression (see
471 .Xr re_format 7 ) .
473 A line can be split by substituting a newline character into it.
474 To specify a newline character in the replacement string, precede it with
475 a backslash.
477 The value of
478 .Em flags
479 in the substitute function is zero or more of the following:
480 .Bl -tag -width "XXXXXX" -offset indent
481 .It Ar N
482 Make the substitution only for the
483 .Ar N Ns 'th
484 occurrence of the regular expression in the pattern space.
485 .It g
486 Make the substitution for all non-overlapping matches of the
487 regular expression, not just the first one.
488 .It p
489 Write the pattern space to standard output if a replacement was made.
490 If the replacement string is identical to that which it replaces, it
491 is still considered to have been a replacement.
492 .It w Em file
493 Append the pattern space to
494 .Em file
495 if a replacement was made.
496 If the replacement string is identical to that which it replaces, it
497 is still considered to have been a replacement.
498 .It I
499 Match the regular expression in a case-insensitive way.
502 .It [2addr]t [label]
503 Branch to the
504 .Dq \&:
505 function bearing the label if any substitutions have been made since the
506 most recent reading of an input line or execution of a
507 .Dq t
508 function.
509 If no label is specified, branch to the end of the script.
511 .It [2addr]w Em file
512 Append the pattern space to the
513 .Em file .
515 .It [2addr]x
516 Swap the contents of the pattern and hold spaces.
518 .It [2addr]y/string1/string2/
519 Replace all occurrences of characters in
520 .Em string1
521 in the pattern space with the corresponding characters from
522 .Em string2 .
523 Any character other than a backslash or newline can be used instead of
524 a slash to delimit the strings.
525 Within
526 .Em string1
528 .Em string2 ,
529 a backslash followed by any character other than a newline is that literal
530 character, and a backslash followed by an ``n'' is replaced by a newline
531 character.
533 .It [2addr]!function
534 .It [2addr]!function-list
535 Apply the function or function-list only to the lines that are
536 .Em not
537 selected by the address(es).
539 .It [0addr]:label
540 This function does nothing; it bears a label to which the
541 .Dq b
543 .Dq t
544 commands may branch.
546 .It [1addr]=
547 Write the line number to the standard output followed by a newline
548 character.
550 .It [0addr]
551 Empty lines are ignored.
553 .It [0addr]#
555 .Dq #
556 and the remainder of the line are ignored (treated as a comment), with
557 the single exception that if the first two characters in the file are
558 .Dq #n ,
559 the default output is suppressed.
560 This is the same as specifying the
561 .Fl n
562 option on the command line.
564 .Sh ENVIRONMENT
566 .Ev COLUMNS , LANG , LC_ALL , LC_CTYPE
568 .Ev LC_COLLATE
569 environment variables affect the execution of
571 as 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
584 utility is expected to be a superset of the
585 .St -p1003.2
586 specification.
589 .Fl E , I , a
591 .Fl i
592 options, as well as the
593 .Dq I
594 flag to the address regular expression and substitution command are
595 non-standard
597 extensions and may not be available on other operating systems.
598 .Sh HISTORY
601 command, written by
602 .An L. E. McMahon ,
603 appeared in
604 .At v7 .
605 .Sh AUTHORS
606 .An "Diomidis D. Spinellis" Aq dds@FreeBSD.org
607 .Sh BUGS
608 Multibyte characters containing a byte with value 0x5C
609 .Tn ( ASCII
610 .Ql \e )
611 may be incorrectly treated as line continuation characters in arguments to the
612 .Dq a ,
613 .Dq c
615 .Dq i
616 commands.
617 Multibyte characters cannot be used as delimiters with the
618 .Dq s
620 .Dq y
621 commands.