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