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