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