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