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 May 7, 2002 39.Dt SED 1 40.Os 41.Sh NAME 42.Nm sed 43.Nd stream editor 44.Sh SYNOPSIS 45.Nm 46.Op Fl Ean 47.Ar command 48.Op Ar 49.Nm 50.Op Fl Ean 51.Op Fl e Ar command 52.Op Fl f Ar command_file 53.Op Fl i Ar extension 54.Op Ar 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 E 75Interpret regular expressions as extended (modern) regular expressions 76rather than basic regular expressions (BRE's). 77The 78.Xr re_format 7 79manual page fully describes both formats. 80.It Fl a 81The files listed as parameters for the 82.Dq w 83functions are created (or truncated) before any processing begins, 84by default. 85The 86.Fl a 87option causes 88.Nm 89to delay opening each file until a command containing the related 90.Dq w 91function is applied to a line of input. 92.It Fl e Ar command 93Append the editing commands specified by the 94.Ar command 95argument 96to the list of commands. 97.It Fl f Ar command_file 98Append the editing commands found in the file 99.Ar command_file 100to the list of commands. 101The editing commands should each be listed on a separate line. 102.It Fl i Ar extension 103Edit files in-place, saving backups with the specified 104.Ar extension . 105If a zero-length 106.Ar extension 107is given, no backup will be saved. 108It is not recommended to give a zero-length 109.Ar extension 110when in-place editing files, as you risk corruption or partial content 111in situations where disk space is exhausted, etc. 112.It Fl n 113By default, each line of input is echoed to the standard output after 114all of the commands have been applied to it. 115The 116.Fl n 117option suppresses this behavior. 118.El 119.Pp 120The form of a 121.Nm 122command is as follows: 123.Pp 124.Dl [address[,address]]function[arguments] 125.Pp 126Whitespace may be inserted before the first address and the function 127portions of the command. 128.Pp 129Normally, 130.Nm 131cyclically copies a line of input, not including its terminating newline 132character, into a 133.Em "pattern space" , 134(unless there is something left after a 135.Dq D 136function), 137applies all of the commands with addresses that select that pattern space, 138copies the pattern space to the standard output, appending a newline, and 139deletes the pattern space. 140.Pp 141Some of the functions use a 142.Em "hold space" 143to save all or part of the pattern space for subsequent retrieval. 144.Sh "Sed Addresses" 145An address is not required, but if specified must be a number (that counts 146input lines 147cumulatively across input files), a dollar 148.Pq Dq $ 149character that addresses the last line of input, or a context address 150(which consists of a regular expression preceded and followed by a 151delimiter). 152.Pp 153A command line with no addresses selects every pattern space. 154.Pp 155A command line with one address selects all of the pattern spaces 156that match the address. 157.Pp 158A command line with two addresses selects an inclusive range. This 159range starts with the first pattern space that matches the first 160address. The end of the range is the next following pattern space 161that matches the second address. If the second address is a number 162less than or equal to the line number first selected, only that 163line is selected. In the case when the second address is a context 164address, 165.Nm 166does not re-match the second address against the 167pattern space that matched the first address. Starting at the 168first line following the selected range, 169.Nm 170starts looking again for the first address. 171.Pp 172Editing commands can be applied to non-selected pattern spaces by use 173of the exclamation character 174.Pq Dq \&! 175function. 176.Sh "Sed Regular Expressions" 177The regular expressions used in 178.Nm , 179by default, are basic regular expressions (BREs, see 180.Xr re_format 7 181for more information). 182.Nm 183can use extended (modern) regular expressions instead if the 184.Fl E 185flag is given. 186In addition, 187.Nm 188has the following two additions to regular expressions: 189.Pp 190.Bl -enum -compact 191.It 192In a context address, any character other than a backslash 193.Pq Dq \e 194or newline character may be used to delimit the regular expression. 195Also, putting a backslash character before the delimiting character 196causes the character to be treated literally. 197For example, in the context address \exabc\exdefx, the RE delimiter 198is an 199.Dq x 200and the second 201.Dq x 202stands for itself, so that the regular expression is 203.Dq abcxdef . 204.Pp 205.It 206The escape sequence \en matches a newline character embedded in the 207pattern space. 208You can't, however, use a literal newline character in an address or 209in the substitute command. 210.El 211.Pp 212One special feature of 213.Nm 214regular expressions is that they can default to the last regular 215expression used. 216If a regular expression is empty, i.e. just the delimiter characters 217are specified, the last regular expression encountered is used instead. 218The last regular expression is defined as the last regular expression 219used as part of an address or substitute command, and at run-time, not 220compile-time. 221For example, the command 222.Dq /abc/s//XXX/ 223will substitute 224.Dq XXX 225for the pattern 226.Dq abc . 227.Sh "Sed Functions" 228In the following list of commands, the maximum number of permissible 229addresses for each command is indicated by [0addr], [1addr], or [2addr], 230representing zero, one, or two addresses. 231.Pp 232The argument 233.Em text 234consists of one or more lines. 235To embed a newline in the text, precede it with a backslash. 236Other backslashes in text are deleted and the following character 237taken literally. 238.Pp 239The 240.Dq r 241and 242.Dq w 243functions take an optional file parameter, which should be separated 244from the function letter by white space. 245Each file given as an argument to 246.Nm 247is created (or its contents truncated) before any input processing begins. 248.Pp 249The 250.Dq b , 251.Dq r , 252.Dq s , 253.Dq t , 254.Dq w , 255.Dq y , 256.Dq \&! , 257and 258.Dq \&: 259functions all accept additional arguments. 260The following synopses indicate which arguments have to be separated from 261the function letters by white space characters. 262.Pp 263Two of the functions take a function-list. 264This is a list of 265.Nm 266functions separated by newlines, as follows: 267.Bd -literal -offset indent 268{ function 269 function 270 ... 271 function 272} 273.Ed 274.Pp 275The 276.Dq { 277can be preceded by white space and can be followed by white space. 278The function can be preceded by white space. 279The terminating 280.Dq } 281must be preceded by a newline or optional white space. 282.Pp 283.Bl -tag -width "XXXXXX" -compact 284.It [2addr] function-list 285Execute function-list only when the pattern space is selected. 286.Pp 287.It [1addr]a\e 288.It text 289Write 290.Em text 291to standard output immediately before each attempt to read a line of input, 292whether by executing the 293.Dq N 294function or by beginning a new cycle. 295.Pp 296.It [2addr]b[label] 297Branch to the 298.Dq \&: 299function with the specified label. 300If the label is not specified, branch to the end of the script. 301.Pp 302.It [2addr]c\e 303.It text 304Delete the pattern space. 305With 0 or 1 address or at the end of a 2-address range, 306.Em text 307is written to the standard output. 308.Pp 309.It [2addr]d 310Delete the pattern space and start the next cycle. 311.Pp 312.It [2addr]D 313Delete the initial segment of the pattern space through the first 314newline character and start the next cycle. 315.Pp 316.It [2addr]g 317Replace the contents of the pattern space with the contents of the 318hold space. 319.Pp 320.It [2addr]G 321Append a newline character followed by the contents of the hold space 322to the pattern space. 323.Pp 324.It [2addr]h 325Replace the contents of the hold space with the contents of the 326pattern space. 327.Pp 328.It [2addr]H 329Append a newline character followed by the contents of the pattern space 330to the hold space. 331.Pp 332.It [1addr]i\e 333.It text 334Write 335.Em text 336to the standard output. 337.Pp 338.It [2addr]l 339(The letter ell.) 340Write the pattern space to the standard output in a visually unambiguous 341form. 342This form is as follows: 343.Pp 344.Bl -tag -width "carriage-returnXX" -offset indent -compact 345.It backslash 346\e\e 347.It alert 348\ea 349.It form-feed 350\ef 351.It carriage-return 352\er 353.It tab 354\et 355.It vertical tab 356\ev 357.El 358.Pp 359Nonprintable characters are written as three-digit octal numbers (with a 360preceding backslash) for each byte in the character (most significant byte 361first). 362Long lines are folded, with the point of folding indicated by displaying 363a backslash followed by a newline. 364The end of each line is marked with a 365.Dq $ . 366.Pp 367.It [2addr]n 368Write the pattern space to the standard output if the default output has 369not been suppressed, and replace the pattern space with the next line of 370input. 371.Pp 372.It [2addr]N 373Append the next line of input to the pattern space, using an embedded 374newline character to separate the appended material from the original 375contents. 376Note that the current line number changes. 377.Pp 378.It [2addr]p 379Write the pattern space to standard output. 380.Pp 381.It [2addr]P 382Write the pattern space, up to the first newline character to the 383standard output. 384.Pp 385.It [1addr]q 386Branch to the end of the script and quit without starting a new cycle. 387.Pp 388.It [1addr]r file 389Copy the contents of 390.Em file 391to the standard output immediately before the next attempt to read a 392line of input. 393If 394.Em file 395cannot be read for any reason, it is silently ignored and no error 396condition is set. 397.Pp 398.It [2addr]s/regular expression/replacement/flags 399Substitute the replacement string for the first instance of the regular 400expression in the pattern space. 401Any character other than backslash or newline can be used instead of 402a slash to delimit the RE and the replacement. 403Within the RE and the replacement, the RE delimiter itself can be used as 404a literal character if it is preceded by a backslash. 405.Pp 406An ampersand 407.Pq Dq & 408appearing in the replacement is replaced by the string matching the RE. 409The special meaning of 410.Dq & 411in this context can be suppressed by preceding it by a backslash. 412The string 413.Dq \e# , 414where 415.Dq # 416is a digit, is replaced by the text matched 417by the corresponding backreference expression (see 418.Xr re_format 7 ) . 419.Pp 420A line can be split by substituting a newline character into it. 421To specify a newline character in the replacement string, precede it with 422a backslash. 423.Pp 424The value of 425.Em flags 426in the substitute function is zero or more of the following: 427.Bl -tag -width "XXXXXX" -offset indent 428.It Ar N 429Make the substitution only for the 430.Ar N Ns 'th 431occurrence of the regular expression 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.Pp 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.Pp 456.It [2addr]w Em file 457Append the pattern space to the 458.Em file . 459.Pp 460.It [2addr]x 461Swap the contents of the pattern and hold spaces. 462.Pp 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.Pp 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.Pp 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.Pp 491.It [1addr]= 492Write the line number to the standard output followed by a newline 493character. 494.Pp 495.It [0addr] 496Empty lines are ignored. 497.Pp 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 ENVIRONMENT 510The 511.Ev COLUMNS , LANG , LC_ALL , LC_CTYPE 512and 513.Ev LC_COLLATE 514environment variables affect the execution of 515.Nm 516as described in 517.Xr environ 7 . 518.Sh DIAGNOSTICS 519.Ex -std 520.Sh SEE ALSO 521.Xr awk 1 , 522.Xr ed 1 , 523.Xr grep 1 , 524.Xr regex 3 , 525.Xr re_format 7 526.Sh STANDARDS 527The 528.Nm 529utility is expected to be a superset of the 530.St -p1003.2 531specification. 532.Pp 533The 534.Fl i 535option is a non-standard 536.Fx 537extension and may not be available on other operating systems. 538.Sh HISTORY 539A 540.Nm 541command, written by L. E. McMahon, appeared in 542.At v7 . 543.Sh AUTHORS 544.An "Diomidis D. Spinellis" Aq dds@FreeBSD.org 545