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