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