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