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