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