1.\" $OpenBSD: sed.1,v 1.49 2016/10/26 00:46:01 schwarze 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: October 26 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 Ar N 446Make the substitution only for the 447.Ar N Ap th 448occurrence of the regular expression in the pattern space, where 449.Ar N 450is a positive integer starting with 451.Cm 1 No ... Cm 9 . 452.It Cm g 453Make the substitution for all non-overlapping matches of the 454regular expression, not just the first one. 455.It Cm p 456Write the pattern space to standard output if a replacement was made. 457If the replacement string is identical to that which it replaces, it 458is still considered to have been a replacement. 459.It Cm w Ar file 460Append the pattern space to 461.Ar file 462if a replacement was made. 463If the replacement string is identical to that which it replaces, it 464is still considered to have been a replacement. 465.El 466.It [2addr] Ns Ic t Bq Ar label 467Branch to the 468.Ic \&: 469function bearing the 470.Ar label 471if any substitutions have been made since the 472most recent reading of an input line or execution of a 473.Ic t 474function. 475If no label is specified, branch to the end of the script. 476.It [2addr] Ns Ic w Ar file 477Append the pattern space to the 478.Ar file . 479.It [2addr] Ns Ic x 480Swap the contents of the pattern and hold spaces. 481.It [2addr] Ns Ic y Ns / Ns Ar string1 Ns / Ns Ar string2 Ns / 482Replace all occurrences of characters in 483.Ar string1 484in the pattern space with the corresponding characters from 485.Ar string2 . 486Any character other than a backslash or newline can be used instead of 487a slash to delimit the strings. 488Within 489.Ar string1 490and 491.Ar string2 , 492a backslash followed by any character other than a newline is that literal 493character, and a backslash followed by an 494.Sq n 495is replaced by a newline character. 496.It [0addr] Ns Ic \&: Ns Ar label 497This function does nothing; it bears a 498.Ar label 499to which the 500.Ic b 501and 502.Ic t 503commands may branch. 504.It [1addr] Ns Ic = 505Write the line number to the standard output followed by a newline character. 506.It [0addr] 507Empty lines are ignored. 508.It [0addr] Ns Ic # 509The 510.Ql # 511and the remainder of the line are ignored (treated as a comment), with 512the single exception that if the first two characters in the file are 513.Ql #n , 514the default output is suppressed. 515This is the same as specifying the 516.Fl n 517option on the command line. 518.El 519.Sh ENVIRONMENT 520.Bl -tag -width COLUMNS 521.It Ev COLUMNS 522If set to a positive integer, 523output from the 524.Ic l 525function is formatted to the given width in columns. 526Otherwise, 527.Nm 528defaults to the terminal with, or 80 columns if the output is not a terminal. 529.El 530.Sh EXIT STATUS 531.Ex -std sed 532.Sh EXAMPLES 533The following simulates the 534.Xr cat 1 535.Fl s 536command, 537squeezing excess empty lines from standard input: 538.Bd -literal -offset indent 539$ sed -n ' 540# Write non-empty lines. 541/./ { 542 p 543 d 544 } 545# Write a single empty line, then look for more empty lines. 546/^$/ p 547# Get the next line, discard the held <newline> (empty line), 548# and look for more empty lines. 549:Empty 550/^$/ { 551 N 552 s/.// 553 b Empty 554 } 555# Write the non-empty line before going back to search 556# for the first in a set of empty lines. 557 p 558\&' 559.Ed 560.Sh SEE ALSO 561.Xr awk 1 , 562.Xr ed 1 , 563.Xr grep 1 , 564.Xr re_format 7 565.Sh STANDARDS 566The 567.Nm 568utility is compliant with the 569.St -p1003.1-2008 570specification. 571.Pp 572The flags 573.Op Fl aEiru 574are extensions to that specification. 575.Pp 576The use of newlines to separate multiple commands on the command line 577is non-portable; 578the use of newlines to separate multiple commands within a command file 579.Pq Fl f Ar command_file 580is portable. 581.Sh HISTORY 582A 583.Nm 584command appeared in 585.At v7 . 586.Sh CAVEATS 587The use of semicolons to separate multiple commands 588is not permitted for the following commands: 589.Ic a , b , c , 590.Ic i , r , t , 591.Ic w , \&: , 592and 593.Ic # . 594