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