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