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