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. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)sed.1 8.2 (Berkeley) 12/30/93 36.\" $FreeBSD$ 37.\" 38.Dd December 30, 1993 39.Dt SED 1 40.Os 41.Sh NAME 42.Nm sed 43.Nd stream editor 44.Sh SYNOPSIS 45.Nm sed 46.Op Fl an 47.Ar command 48.Op Ar file ... 49.Nm sed 50.Op Fl an 51.Op Fl e Ar command 52.Op Fl f Ar command_file 53.Op Ar file ... 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 sed . 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 a 74The files listed as parameters for the 75.Dq w 76functions are created (or truncated) before any processing begins, 77by default. 78The 79.Fl a 80option causes 81.Nm 82to delay opening each file until a command containing the related 83.Dq w 84function is applied to a line of input. 85.It Fl e Ar command 86Append the editing commands specified by the 87.Ar command 88argument 89to the list of commands. 90.It Fl f Ar command_file 91Append the editing commands found in the file 92.Ar command_file 93to the list of commands. 94The editing commands should each be listed on a separate line. 95.It Fl n 96By default, each line of input is echoed to the standard output after 97all of the commands have been applied to it. 98The 99.Fl n 100option suppresses this behavior. 101.El 102.Pp 103The form of a 104.Nm 105command is as follows: 106.sp 107.Dl [address[,address]]function[arguments] 108.sp 109Whitespace may be inserted before the first address and the function 110portions of the command. 111.Pp 112Normally, 113.Nm 114cyclically copies a line of input, not including its terminating newline 115character, into a 116.Em "pattern space" , 117(unless there is something left after a 118.Dq D 119function), 120applies all of the commands with addresses that select that pattern space, 121copies the pattern space to the standard output, appending a newline, and 122deletes the pattern space. 123.Pp 124Some of the functions use a 125.Em "hold space" 126to save all or part of the pattern space for subsequent retrieval. 127.Sh "Sed Addresses" 128An address is not required, but if specified must be a number (that counts 129input lines 130cumulatively across input files), a dollar 131.Po 132.Dq $ 133.Pc 134character that addresses the last line of input, or a context address 135(which consists of a regular expression preceded and followed by a 136delimiter). 137.Pp 138A command line with no addresses selects every pattern space. 139.Pp 140A command line with one address selects all of the pattern spaces 141that match the address. 142.Pp 143A command line with two addresses selects an inclusive range. This 144range starts with the first pattern space that matches the first 145address. The end of the range is the next following pattern space 146that matches the second address. If the second address is a number 147less than or equal to the line number first selected, only that 148line is selected. In the case when the second address is a context 149address, sed does not re-match the second address against the 150pattern space that matched the first address. Starting at the 151first line following the selected range, sed starts looking again 152for the first address. 153.Nm 154starts looking again for the first address. 155.Pp 156Editing commands can be applied to non-selected pattern spaces by use 157of the exclamation character 158.Po 159.Dq ! 160.Pc 161function. 162.Sh "Sed Regular Expressions" 163The 164.Nm 165regular expressions are basic regular expressions (BRE's, see 166.Xr regex 3 167for more information). 168In addition, 169.Nm 170has the following two additions to BRE's: 171.sp 172.Bl -enum -compact 173.It 174In a context address, any character other than a backslash 175.Po 176.Dq \e 177.Pc 178or newline character may be used to delimit the regular expression. 179Also, putting a backslash character before the delimiting character 180causes the character to be treated literally. 181For example, in the context address \exabc\exdefx, the RE delimiter 182is an 183.Dq x 184and the second 185.Dq x 186stands for itself, so that the regular expression is 187.Dq abcxdef . 188.sp 189.It 190The escape sequence \en matches a newline character embedded in the 191pattern space. 192You can't, however, use a literal newline character in an address or 193in the substitute command. 194.El 195.Pp 196One special feature of 197.Nm 198regular expressions is that they can default to the last regular 199expression used. 200If a regular expression is empty, i.e. just the delimiter characters 201are specified, the last regular expression encountered is used instead. 202The last regular expression is defined as the last regular expression 203used as part of an address or substitute command, and at run-time, not 204compile-time. 205For example, the command 206.Dq /abc/s//XXX/ 207will substitute 208.Dq XXX 209for the pattern 210.Dq abc . 211.Sh "Sed Functions" 212In the following list of commands, the maximum number of permissible 213addresses for each command is indicated by [0addr], [1addr], or [2addr], 214representing zero, one, or two addresses. 215.Pp 216The argument 217.Em text 218consists of one or more lines. 219To embed a newline in the text, precede it with a backslash. 220Other backslashes in text are deleted and the following character 221taken literally. 222.Pp 223The 224.Dq r 225and 226.Dq w 227functions take an optional file parameter, which should be separated 228from the function letter by white space. 229Each file given as an argument to 230.Nm 231is created (or its contents truncated) before any input processing begins. 232.Pp 233The 234.Dq b , 235.Dq r , 236.Dq s , 237.Dq t , 238.Dq w , 239.Dq y , 240.Dq ! , 241and 242.Dq \&: 243functions all accept additional arguments. 244The following synopses indicate which arguments have to be separated from 245the function letters by white space characters. 246.Pp 247Two of the functions take a function-list. 248This is a list of 249.Nm 250functions separated by newlines, as follows: 251.Bd -literal -offset indent 252{ function 253 function 254 ... 255 function 256} 257.Ed 258.Pp 259The 260.Dq { 261can be preceded by white space and can be followed by white space. 262The function can be preceded by white space. 263The terminating 264.Dq } 265must be preceded by a newline or optional white space. 266.sp 267.Bl -tag -width "XXXXXX" -compact 268.It [2addr] function-list 269Execute function-list only when the pattern space is selected. 270.sp 271.It [1addr]a\e 272.It text 273.br 274Write 275.Em text 276to standard output immediately before each attempt to read a line of input, 277whether by executing the 278.Dq N 279function or by beginning a new cycle. 280.sp 281.It [2addr]b[label] 282Branch to the 283.Dq \&: 284function with the specified label. 285If the label is not specified, branch to the end of the script. 286.sp 287.It [2addr]c\e 288.It text 289.br 290Delete the pattern space. 291With 0 or 1 address or at the end of a 2-address range, 292.Em text 293is written to the standard output. 294.sp 295.It [2addr]d 296Delete the pattern space and start the next cycle. 297.sp 298.It [2addr]D 299Delete the initial segment of the pattern space through the first 300newline character and start the next cycle. 301.sp 302.It [2addr]g 303Replace the contents of the pattern space with the contents of the 304hold space. 305.sp 306.It [2addr]G 307Append a newline character followed by the contents of the hold space 308to the pattern space. 309.sp 310.It [2addr]h 311Replace the contents of the hold space with the contents of the 312pattern space. 313.sp 314.It [2addr]H 315Append a newline character followed by the contents of the pattern space 316to the hold space. 317.sp 318.It [1addr]i\e 319.It text 320.br 321Write 322.Em text 323to the standard output. 324.sp 325.It [2addr]l 326(The letter ell.) 327Write the pattern space to the standard output in a visually unambiguous 328form. 329This form is as follows: 330.sp 331.Bl -tag -width "carriage-returnXX" -offset indent -compact 332.It backslash 333\e\e 334.It alert 335\ea 336.It form-feed 337\ef 338.It newline 339\en 340.It carriage-return 341\er 342.It tab 343\et 344.It vertical tab 345\ev 346.El 347.Pp 348Nonprintable characters are written as three-digit octal numbers (with a 349preceding backslash) for each byte in the character (most significant byte 350first). 351Long lines are folded, with the point of folding indicated by displaying 352a backslash followed by a newline. 353The end of each line is marked with a 354.Dq $ . 355.sp 356.It [2addr]n 357Write the pattern space to the standard output if the default output has 358not been suppressed, and replace the pattern space with the next line of 359input. 360.sp 361.It [2addr]N 362Append the next line of input to the pattern space, using an embedded 363newline character to separate the appended material from the original 364contents. 365Note that the current line number changes. 366.sp 367.It [2addr]p 368Write the pattern space to standard output. 369.sp 370.It [2addr]P 371Write the pattern space, up to the first newline character to the 372standard output. 373.sp 374.It [1addr]q 375Branch to the end of the script and quit without starting a new cycle. 376.sp 377.It [1addr]r file 378Copy the contents of 379.Em file 380to the standard output immediately before the next attempt to read a 381line of input. 382If 383.Em file 384cannot be read for any reason, it is silently ignored and no error 385condition is set. 386.sp 387.It [2addr]s/regular expression/replacement/flags 388Substitute the replacement string for the first instance of the regular 389expression in the pattern space. 390Any character other than backslash or newline can be used instead of 391a slash to delimit the RE and the replacement. 392Within the RE and the replacement, the RE delimiter itself can be used as 393a literal character if it is preceded by a backslash. 394.Pp 395An ampersand 396.Po 397.Dq & 398.Pc 399appearing in the replacement is replaced by the string matching the RE. 400The special meaning of 401.Dq & 402in this context can be suppressed by preceding it by a backslash. 403The string 404.Dq \e# , 405where 406.Dq # 407is a digit, is replaced by the text matched 408by the corresponding backreference expression (see 409.Xr re_format 7 ). 410.Pp 411A line can be split by substituting a newline character into it. 412To specify a newline character in the replacement string, precede it with 413a backslash. 414.Pp 415The value of 416.Em flags 417in the substitute function is zero or more of the following: 418.Bl -tag -width "XXXXXX" -offset indent 419.It "0 ... 9" 420Make the substitution only for the N'th occurrence of the regular 421expression in the pattern space. 422.It g 423Make the substitution for all non-overlapping matches of the 424regular expression, not just the first one. 425.It p 426Write the pattern space to standard output if a replacement was made. 427If the replacement string is identical to that which it replaces, it 428is still considered to have been a replacement. 429.It w Em file 430Append the pattern space to 431.Em file 432if a replacement was made. 433If the replacement string is identical to that which it replaces, it 434is still considered to have been a replacement. 435.El 436.sp 437.It [2addr]t [label] 438Branch to the 439.Dq \&: 440function bearing the label if any substitutions have been made since the 441most recent reading of an input line or execution of a 442.Dq t 443function. 444If no label is specified, branch to the end of the script. 445.sp 446.It [2addr]w Em file 447Append the pattern space to the 448.Em file . 449.sp 450.It [2addr]x 451Swap the contents of the pattern and hold spaces. 452.sp 453.It [2addr]y/string1/string2/ 454Replace all occurrences of characters in 455.Em string1 456in the pattern space with the corresponding characters from 457.Em string2 . 458Any character other than a backslash or newline can be used instead of 459a slash to delimit the strings. 460Within 461.Em string1 462and 463.Em string2 , 464a backslash followed by any character other than a newline is that literal 465character, and a backslash followed by an ``n'' is replaced by a newline 466character. 467.sp 468.It [2addr]!function 469.It [2addr]!function-list 470Apply the function or function-list only to the lines that are 471.Em not 472selected by the address(es). 473.sp 474.It [0addr]:label 475This function does nothing; it bears a label to which the 476.Dq b 477and 478.Dq t 479commands may branch. 480.sp 481.It [1addr]= 482Write the line number to the standard output followed by a newline 483character. 484.sp 485.It [0addr] 486Empty lines are ignored. 487.sp 488.It [0addr]# 489The 490.Dq # 491and the remainder of the line are ignored (treated as a comment), with 492the single exception that if the first two characters in the file are 493.Dq #n , 494the default output is suppressed. 495This is the same as specifying the 496.Fl n 497option on the command line. 498.El 499.Pp 500The 501.Nm 502utility exits 0 on success and >0 if an error occurs. 503.Sh SEE ALSO 504.Xr awk 1 , 505.Xr ed 1 , 506.Xr grep 1 , 507.Xr regex 3 , 508.Xr re_format 7 509.Sh HISTORY 510A 511.Nm 512command appeared in 513.At v7 . 514.Sh STANDARDS 515The 516.Nm 517function is expected to be a superset of the 518.St -p1003.2 519specification. 520