1.\" $OpenBSD: sed.1,v 1.58 2018/12/10 17:12:24 schwarze Exp $ 2.\" 3.\" Copyright (c) 1992, 1993 4.\" The Regents of the University of California. 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.\" 3. 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.\" from: @(#)sed.1 8.2 (Berkeley) 12/30/93 34.\" 35.Dd $Mdocdate: December 10 2018 $ 36.Dt SED 1 37.Os 38.Sh NAME 39.Nm sed 40.Nd stream editor 41.Sh SYNOPSIS 42.Nm sed 43.Op Fl aEnru 44.Op Fl i Ns Op Ar extension 45.Ar command 46.Op Ar 47.Nm sed 48.Op Fl aEnru 49.Op Fl e Ar command 50.Op Fl f Ar command_file 51.Op Fl i Ns Op Ar extension 52.Op Ar 53.Sh DESCRIPTION 54The 55.Nm 56utility reads the specified files, or the standard input if no files 57are specified, modifying the input as specified by a list of commands. 58The input is then written to the standard output. 59.Pp 60A single command may be specified as the first argument to 61.Nm sed . 62Multiple commands may be specified 63separated by newlines or semicolons, 64or by using the 65.Fl e 66or 67.Fl f 68options. 69All commands are applied to the input in the order they are specified 70regardless of their origin. 71.Pp 72The options are as follows: 73.Bl -tag -width Ds 74.It Fl a 75The files listed as parameters for the 76.Ic w 77function or flag are created (or truncated) before any processing begins, 78by default. 79The 80.Fl a 81option causes 82.Nm 83to delay opening each file until a command containing the related 84.Ic w 85function or flag is applied to a line of input. 86.It Fl E 87Interpret regular expressions using POSIX extended regular expression syntax. 88The default behaviour is to use POSIX basic regular expression syntax. 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 Ns Op 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 it risks corruption or partial content 108in situations where disk space is exhausted, etc. 109In 110.Fl i 111mode, the hold space, line numbers, and ranges are reset between files. 112.It Fl r 113An alias for 114.Fl E , 115for compatibility with GNU sed. 116.It Fl n 117By default, each line of input is echoed to the standard output after 118all of the commands have been applied to it. 119The 120.Fl n 121option suppresses this behavior. 122.It Fl u 123Force output to be line buffered, 124printing each line as it becomes available. 125By default, output is line buffered when standard output is a terminal 126and block buffered otherwise. 127See 128.Xr setvbuf 3 129for a more detailed explanation. 130.El 131.Pp 132The form of a 133.Nm 134command is as follows: 135.Pp 136.Dl [address[,address]]function[arguments] 137.Pp 138Whitespace may be inserted before the first address and the function 139portions of the command. 140.Pp 141Normally, 142.Nm 143cyclically copies a line of input, not including its terminating newline 144character, into a 145.Em pattern space , 146(unless there is something left after a 147.Ic D 148function), 149applies all of the commands with addresses that select that pattern space, 150copies the pattern space to the standard output, appending a newline, and 151deletes the pattern space. 152.Pp 153Some of the functions use a 154.Em hold space 155to save all or part of the pattern space for subsequent retrieval. 156.Sh SED ADDRESSES 157An address is not required, but if specified must be a number (that counts 158input lines 159cumulatively across input files), a dollar character 160.Pq Ql $ 161that addresses the last line of input, or a context address, 162which is a regular expression preceded and followed by a delimiter. 163The delimiter can be any character except a newline or a backslash. 164Unless it is a slash, the opening delimiter needs to be escaped with 165a backslash. 166.Pp 167A command line with no addresses selects every pattern space. 168.Pp 169A command line with one address selects all of the pattern spaces 170that match the address. 171.Pp 172A command line with two addresses selects the inclusive range from 173the first pattern space that matches the first address through the next 174pattern space that matches the second. 175(If the second address is a number less than or equal to the line number 176first selected, only that line is selected.) 177Starting at the first line following the selected range, 178.Nm 179starts looking again for the first address. 180.Pp 181Editing commands can be applied to non-selected pattern spaces by use 182of the exclamation character 183.Pq Ql \&! 184function. 185.Sh SED REGULAR EXPRESSIONS 186By default, 187.Nm 188regular expressions are basic regular expressions 189.Pq BREs . 190Extended regular expressions are supported using the 191.Fl E 192and 193.Fl r 194options. 195See 196.Xr re_format 7 197for more information on regular expressions. 198In addition, 199.Nm 200has the following two additions to BREs: 201.Pp 202.Bl -enum -compact 203.It 204The character delimiting the regular expression 205can be used inside the regular expression by prepending a backslash 206or by including it in a character class. 207For example, in the context address \ex\ex[xy]x, the RE delimiter 208is an 209.Sq x 210and the other 211.Sq x 212characters stand for themselves, so that the regular expression is 213.Dq x[xy] . 214.Pp 215.It 216The escape sequence \en matches a newline character embedded in the 217pattern space. 218You can't, however, use a literal newline character in an address or 219in the substitute command. 220.El 221.Pp 222One special feature of 223.Nm 224regular expressions is that they can default to the last regular 225expression used. 226If a regular expression is empty, i.e., just the delimiter characters 227are specified, the last regular expression encountered is used instead. 228The last regular expression is defined as the last regular expression 229used as part of an address or substitute command, and at run-time, not 230compile-time. 231For example, the command 232.Dq /abc/s//XXX/ 233will substitute 234.Dq XXX 235for the pattern 236.Dq abc . 237.Sh SED FUNCTIONS 238In the following list of commands, the maximum number of permissible 239addresses for each command is indicated by [0addr], [1addr], or [2addr], 240representing zero, one, or two addresses. 241.Pp 242The argument 243.Ar text 244consists of one or more lines. 245To embed a newline in the text, precede it with a backslash. 246Other backslashes in text are deleted and the following character 247taken literally. 248.Pp 249The 250.Ic r 251and 252.Ic w 253functions, 254as well as the 255.Cm w 256flag to the 257.Ic s 258function, 259take a 260.Ar file 261parameter, 262which should be separated from the function or flag by whitespace. 263Files are created 264(or their contents truncated) 265before any input processing begins. 266.Pp 267The 268.Ic b , 269.Ic r , 270.Ic s , 271.Ic t , 272.Ic w , 273.Ic y , 274and 275.Ic \&: 276functions all accept additional arguments. 277The synopses below indicate which arguments have to be separated from 278the function letters by whitespace characters. 279.Pp 280The 281.Ic a , 282.Ic c , 283.Ic i , 284.Ic r , 285and 286.Ic w 287functions cannot be followed by another command separated with a semicolon. 288The 289.Ar text 290and 291.Ar file 292arguments may contain semicolon characters. 293.Pp 294Functions can be combined to form a 295.Em function list , 296a list of 297.Nm 298functions each followed by a newline, as follows: 299.Bd -literal -offset indent 300{ function 301 function 302 ... 303 function 304} 305.Ed 306.Pp 307The braces can be preceded and followed by whitespace. 308The functions can be preceded by whitespace as well. 309.Pp 310Functions and function lists may be preceded by an exclamation mark, 311in which case they are applied only to lines that are 312.Em not 313selected by the addresses. 314.Bl -tag -width Ds 315.It [2addr] Ns Ar function-list 316Execute 317.Ar function-list 318only when the pattern space is selected. 319.It Xo [1addr] Ns Ic a Ns \e 320.br 321.Ar text 322.Xc 323Write 324.Ar text 325to standard output immediately before each attempt to read a line of input, 326whether by executing the 327.Ic N 328function or by beginning a new cycle. 329.It [2addr] Ns Ic b Bq Ar label 330Branch to the 331.Ic \&: 332function with the specified 333.Ar label . 334If the label is not specified, branch to the end of the script. 335.It Xo [2addr] Ns Ic c Ns \e 336.br 337.Ar text 338.Xc 339Delete the pattern space. 340With 0 or 1 address or at the end of a 2-address range, 341.Ar text 342is written to the standard output. 343.It [2addr] Ns Ic d 344Delete the pattern space and start the next cycle. 345.It [2addr] Ns Ic D 346Delete the initial segment of the pattern space through the first 347newline character and start the next cycle. 348.It [2addr] Ns Ic g 349Replace the contents of the pattern space with the contents of the 350hold space. 351.It [2addr] Ns Ic G 352Append a newline character followed by the contents of the hold space 353to the pattern space. 354.It [2addr] Ns Ic h 355Replace the contents of the hold space with the contents of the 356pattern space. 357.It [2addr] Ns Ic H 358Append a newline character followed by the contents of the pattern space 359to the hold space. 360.It Xo [1addr] Ns Ic i Ns \e 361.br 362.Ar text 363.Xc 364Write 365.Ar text 366to the standard output. 367.It [2addr] Ns Ic l 368(The letter ell.) 369Write the pattern space to the standard output in a visually unambiguous 370form. 371This form is as follows: 372.Pp 373.Bl -tag -width "carriage-returnXX" -offset indent -compact 374.It backslash 375\e\e 376.It alert 377\ea 378.It backspace 379\eb 380.It form-feed 381\ef 382.It carriage-return 383\er 384.It tab 385\et 386.It vertical tab 387\ev 388.El 389.Pp 390Non-printable characters are written as three-digit octal numbers (with a 391preceding backslash) for each byte in the character (most significant byte 392first). 393Long lines are folded, with the point of folding indicated by displaying 394a backslash followed by a newline. 395The end of each line is marked with a 396.Ql $ . 397.It [2addr] Ns Ic n 398Write the pattern space to the standard output if the default output has 399not been suppressed, and replace the pattern space with the next line of 400input. 401.It [2addr] Ns Ic N 402Append the next line of input to the pattern space, using an embedded 403newline character to separate the appended material from the original 404contents. 405Note that the current line number changes. 406.It [2addr] Ns Ic p 407Write the pattern space to standard output. 408.It [2addr] Ns Ic P 409Write the pattern space, up to the first newline character, 410to the standard output. 411.It [1addr] Ns Ic q 412Branch to the end of the script and quit without starting a new cycle or file. 413.It [1addr] Ns Ic r Ar file 414Copy the contents of 415.Ar file 416to the standard output immediately before the next attempt to read a 417line of input. 418If 419.Ar file 420cannot be read for any reason, it is silently ignored and no error 421condition is set. 422.It [2addr] Ns Ic s Ns / Ns Ar RE Ns / Ns Ar replacement Ns / Ns Ar flags 423Substitute the 424.Ar replacement 425string for the first instance of the regular expression 426.Ar RE 427in the pattern space. 428Any character other than backslash or newline can be used instead of 429a slash to delimit the regular expression and the replacement. 430Also see the the section about 431.Sx SED REGULAR EXPRESSIONS . 432.Pp 433An ampersand 434.Pq Ql & 435appearing in the replacement is replaced by the string matching the 436regular expression. 437The string 438.Ql \e# , 439where 440.Ql # 441is a digit, is replaced by the text matched 442by the corresponding backreference expression (see 443.Xr re_format 7 ) . 444.Pp 445All other instances of a backslash will print the literal character 446following it. 447Using a backslash before any other character other than 448.Ql & , 449.Ql \e , 450digit, newline 451.Pq ascii 0x0a , 452and the delimiter is unspecified and might not be portable to other 453implementations of 454.Nm . 455.Pp 456The value of 457.Ar flags 458in the substitute function is zero or more of the following: 459.Bl -tag -width "XXXXXX" -offset indent 460.It Ar N 461Make the substitution only for the 462.Ar N Ap th 463occurrence of the regular expression in the pattern space, where 464.Ar N 465is a positive integer starting with 466.Cm 1 No ... Cm 9 . 467.It Cm g 468Make the substitution for all non-overlapping matches of the 469regular expression, not just the first one. 470.It Cm p 471Write the pattern space to standard output if a replacement was made. 472If the replacement string is identical to that which it replaces, it 473is still considered to have been a replacement. 474.It Cm w Ar file 475Append the pattern space to 476.Ar file 477if a replacement was made. 478If the replacement string is identical to that which it replaces, it 479is still considered to have been a replacement. 480.El 481.It [2addr] Ns Ic t Bq Ar label 482Branch to the 483.Ic \&: 484function bearing the 485.Ar label 486if any substitutions have been made since the 487most recent reading of an input line or execution of a 488.Ic t 489function. 490If no label is specified, branch to the end of the script. 491.It [2addr] Ns Ic w Ar file 492Append the pattern space to the 493.Ar file . 494.It [2addr] Ns Ic x 495Swap the contents of the pattern and hold spaces. 496.It [2addr] Ns Ic y Ns / Ns Ar string1 Ns / Ns Ar string2 Ns / 497Replace all occurrences of characters in 498.Ar string1 499in the pattern space with the corresponding characters from 500.Ar string2 . 501Any character other than a backslash or newline can be used instead of 502a slash to delimit the strings. 503.Pp 504Within 505.Ar string1 506and 507.Ar string2 , 508a backslash followed by another backslash 509is replaced by a single backslash, 510a backslash followed by an 511.Sq n 512is replaced by a newline character, 513and a backslash followed by the delimiting character 514is replaced by that character, 515causing it to be treated literally, 516with the exception of the 517.Sq n 518character, 519which will still be treated like a newline character. 520It is an error for a backslash to not be followed by another backslash, 521.Sq n , 522or the delimiting character, 523or for 524.Ar string1 525to contain repeating characters. 526.It [0addr] Ns Ic \&: Ns Ar label 527This function does nothing; it bears a 528.Ar label 529to which the 530.Ic b 531and 532.Ic t 533commands may branch. 534.It [1addr] Ns Ic = 535Write the line number to the standard output followed by a newline character. 536.It [0addr] 537Empty lines are ignored. 538.It [0addr] Ns Ic # 539The 540.Ql # 541and the remainder of the line are ignored (treated as a comment), with 542the single exception that if the first two characters in the file are 543.Ql #n , 544the default output is suppressed. 545This is the same as specifying the 546.Fl n 547option on the command line. 548.El 549.Sh ENVIRONMENT 550.Bl -tag -width COLUMNS 551.It Ev COLUMNS 552If set to a positive integer, 553output from the 554.Ic l 555function is formatted to the given width in columns. 556Otherwise, 557.Nm 558defaults to the terminal width, or 80 columns if the output is not a terminal. 559.El 560.Sh EXIT STATUS 561.Ex -std sed 562.Sh SEE ALSO 563.Xr awk 1 , 564.Xr ed 1 , 565.Xr grep 1 , 566.Xr re_format 7 567.Sh STANDARDS 568The 569.Nm 570utility is compliant with the 571.St -p1003.1-2008 572specification. 573.Pp 574The flags 575.Op Fl aEiru 576are extensions to that specification. 577.Pp 578Following the 579.Ic b , 580.Ic t , 581or 582.Ic \&: 583commands with a semicolon and another command is an extension to the 584specification. 585.Pp 586The use of newlines to separate multiple commands on the command line 587is non-portable; 588the use of newlines to separate multiple commands within a command file 589.Pq Fl f Ar command_file 590is portable. 591.Sh HISTORY 592A 593.Nm 594command appeared in 595.At v7 . 596