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