1.\" Copyright (c) 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" %sccs.include.redist.roff% 5.\" 6.\" @(#)sed.1 6.5 (Berkeley) 04/25/91 7.\" 8.Dd 9.Dt SED 1 10.Os 11.Sh NAME 12.Nm sed 13.Nd stream editor. 14.Sh SYNOPSIS 15.Nm sed 16.Op Fl n 17.Ar script 18.Ar 19.Nm sed 20.Op Fl n 21.Op Fl e Ar script 22.Op Fl f Ar script_file 23.Ar 24.Sh DESCRIPTION 25The 26.Nm sed 27utility is a stream editor which reads one or more 28text files, applies given editing command scripts, 29and writes the results to standard output. 30The script of editing commands can be given in the command 31line, or can be contained in the file 32.Ar script_file . 33.Pp 34Options: 35.Bl -tag -width indent 36.It Fl e Ar script 37The command line 38.Ar script 39is used to edit the input. 40If multiple 41.Fl e 42options are given, the scripts are 43applied in the order given to each line of the 44input files. 45If a 46.Fl f 47option is given in addition 48to 49.Fl e , 50lines are acted upon by scripts first. 51.It Fl f Ar script_file 52The file 53.Ar script_file 54is expected to contain editing commands, one per line, 55and these commands are applied to the input. 56If multiple 57.Fl f 58options are given, the commands in the 59.Ar script_file Ns s 60are applied in 61the order given to each line of the input 62files. 63If a 64.Fl e 65option is given in addition to 66.Fl f , 67lines are acted upon by 68the commands in the 69.At script_file Ns s 70first. 71.It Fl n 72The 73.Fl n 74option suppresses the default output, which normally 75passes each line, after it is examined for editing, 76to standard output. 77Therefore, only lines explicitly 78selected for output are written. 79.El 80.Pp 81The following operands are available: 82.Bl -tag -width file 83.It Ar file 84A pathname of a file whose contents are read and 85edited. 86If multiple file operands are given, 87the named files are read in the order given and 88the concatenation is edited. 89If no file operands 90are given, the standard input is used. 91.It Ar script 92The script consists of one or more editing 93instructions that are entered on the command line. 94.El 95.\" .Pp 96.\" The following environment variable affects the execution of 97.\" sed: 98.\" .It Ev LC_CTYPE 99.\" The locale for character classification. 100.Pp 101.Bd -filled -offset indent -compact 102.Op address Op ,address 103function 104.Op arguments 105.Ed 106.Pp 107In default operation, 108.Nm sed 109cyclically copies a line of input 110into a pattern space (unless there is something left after a 111.Cm D 112command), applies in sequence all commands whose addresses 113select that pattern space, and at the end of the script 114copies the pattern space to the standard output (except 115under 116.Fl n ) 117and deletes the pattern space. 118.Pp 119Some of the commands use a hold space to save all or part of 120the pattern space for 121subsequent retrieval. 122.\" The pattern and hold spaces are each 123.\" limited to 124.\" .Pf { Dv SED_PATTERN_MAX Ns } 125.\" bytes. 126.Pp 127An address is either no address; a decimal number that 128counts input lines cumulatively across files; a 129.Ql $ 130that 131addresses the last line of input; a context address; or 132regular expression. 133.Pp 134A command line with no addresses selects every pattern 135space. 136.Pp 137A command line with one address selects each pattern space 138that matches the address. 139.Pp 140A command line with two addresses selects the inclusive 141range from the first pattern space that matches the first 142address through the next pattern space which matches the 143second. 144(If the second address is a number less than or 145equal to the line number first selected, only one line is 146selected.) 147Starting at the first line following the 148selected range, 149.Nm sed 150looks again for the first address. 151Thereafter the process is repeated. 152.Pp 153Editing commands can be applied only to non-selected pattern 154spaces by use of the negation function 155.Cm \&! 156(See below.) 157.Pp 158The 159.Nm sed 160utility uses basic regular expressions, as are found in the 161editor 162.Xr ed 1 , 163with the following additions: 164.Pp 165.Bl -enum -offset indent 166.It 167In a context address, the construction \e?RE?, where ? 168is any character, is identical to /RE/. Note that in the 169context address \exabc\exdefx, the second x stands for 170itself, so that the regular expression is abcxdef. 171.It 172The escape sequence \en matches a <newline> embedded 173in the pattern space. 174.It 175A period (.) matches any character except the termi- 176nal <newline> of the pattern space. 177.El 178.Pp 179In the following list of functions the maximum number of 180permissible addresses for each function is indicated by 181.Op 0addr , 182.Op 1addr , 183or 184.Op 2addr ; 185representing zero, one, or two 186addresses. 187.Pp 188The argument text consists of one or more lines. 189Each 190embedded <newline> in the text shall be preceded by a 191backslash. 192Other backslashes in text are treated like 193backslashes in the replacement string of an s command, and 194can be used to protect initial <blank>s against the stripping 195that is done on every script line. 196.Pp 197The 198.Cm r 199and 200.Cm w 201commands take an optional 202.Ar rfile 203(or 204.Ar wfile ) 205parameter, separated from the command letter by zero or more 206<blank>s. 207.Pp 208The argument 209.Ar rfile 210or the argument 211.Ar wfile 212shall terminate the 213command line. 214Each 215.Ar wfile 216is created before processing 217begins. 218There can be at most ten distinct 219.Ar wfile 220arguments 221in the script. 222.Pp 223The 224.Cm b , r , s , t , 225.Cm w , y , \&! , 226and 227.Cm \&: 228commands take additional 229arguments. 230The following synopses indicate which arguments 231are separated from the commands by <blank>s. 232.Bl -tag -width addrcommandxx 233.It Oo Ad 2addr Oc \&{ command_list \&} 234Executes command_list only when the pattern 235space is selected. 236The {} braces can be preceded 237and followed by white space. 238.It Oo Ad 1addr Oc Ns Cm a Ar text 239Writes text to the standard output after the 240pattern space is written. 241.It Oo Ad 2addr Oc Ns Cm b Ar label 242Branches to the 243.Cm \&: 244command bearing the label. 245If label is empty, branch to the end of the 246script. 247.It Oo Ad 2addr Oc Ns Cm c Ar text 248Deletes the pattern space. 249With 0 or 1 250address or at the end of a 2-address range, 251places text on the output. 252.It Oo Ad 2addr Oc Ns Cm d 253Deletes the pattern space and starts the next 254cycle. 255.It Oo Ad 2addr Oc Ns Cm D 256Deletes the initial segment of the pattern 257space through the first <newline> and starts 258the next cycle. 259.It Oo Ad 2addr Oc Ns Cm g 260Replaces the contents of the pattern space by 261the contents of the hold space. 262.It Oo Ad 2addr Oc Ns Cm G 263Appends the contents of the hold space to the 264pattern space. 265.It Oo Ad 2addr Oc Ns Cm h 266Replaces the contents of the hold space by 267the contents of the pattern space. 268.It Oo Ad 2addr Oc Ns Cm H 269Appends the contents of the pattern space to 270the hold space. 271.It Oo Ad 1addr Oc Ns Cm i Ar text 272Writes text to the standard output before the 273pattern space is written. 274.It Oo Ad 2addr Oc Ns Cm l 275Lists the pattern space on the standard out- 276put in an unambiguous form. 277Nonprinting 278characters are listed as hexadecimal digit 279pairs, with a preceding backslash, with the 280following exceptions: 281.Pp 282.ne 8 283.Bd -filled -offset indent -compact 284.Bl -column <carriagexreturn> 285<alert> \ea 286<backslash> \e\e 287<backspace> \eb 288<carriage return> \er 289<form-feed> \ef 290<newline> \en 291<tab> \et 292<vertical tab> \ev 293.El 294.Ed 295.Pp 296Long lines are folded; the length at which 297folding occurs is ungiven, but should be 298appropriate for the output device. 299.It Oo Ad 2addr Oc Ns Cm n 300Copies the pattern space to the standard output 301and replaces the pattern space with the 302next line of input. 303.It Oo Ad 2addr Oc Ns Cm N 304Appends the next line of input to the pattern 305space, using an embedded <newline> to 306separate the appended material from the 307original material. 308Note that the current line 309number changes. 310.It Oo Ad 2addr Oc Ns Cm p 311Copies 312.Op prints 313the pattern space to the 314standard output. 315.It Oo Ad 2addr Oc Ns Cm P 316Copies 317.Op prints 318the pattern space, up to the 319first <newline>, to the standard output. 320.It Oo Ad 1addr Oc Ns Cm q 321Branches to the end of the script and quits 322without starting a new cycle. 323.It Oo Ad 1addr Oc Ns Cm r Ar rfile 324Read the contents of rfile. 325Place them on the 326output before reading the next input line. 327.It Oo Ad 2addr Oc Ns \\*(cMs\\*(dF/\\*(aRregular expression\\*(dF/\\*(aRreplacement string\\*(dF/flags 328Substitutes the replacement string for 329instances of the regular expression in the 330pattern space. 331Any character can be used 332instead of 333.Ql / . 334The value of flags is zero or 335more of: 336.Bl -tag -width Ds 337.It Ar n 338n=1-512. Substitutes for the nth 339occurrence only of the regular 340expression found within the pattern space. 341.It Cm g 342Globally substitutes for all 343non-overlapping instances of the regular 344expression rather than just 345the first one. If both 346.Cm g 347and 348.Cm n 349are given, 350.Cm g 351takes precedence. 352.It Cm p 353Prints the pattern space if a 354replacement was made. 355.It Cm w Ar wfile 356Write. Appends the pattern space 357to 358.Ar wfile 359if a replacement was 360made. 361.El 362.It Oo Ad 2addr Oc Ns Cm t Ar label 363Test. 364Branches to the 365.Cm \&: 366command bearing the 367label if any substitutions have been made 368since the most recent reading of an input 369line or execution of a 370.Cm t . 371If label is empty, 372branches to the end of the script. 373.It Oo Ad 2addr Oc Ns Cm w Ar wfile 374Appends 375.Op writes 376the pattern space to 377.Ar wfile . 378.It Oo Ad 2addr Oc Ns Cm x 379Exchanges the contents of the pattern and 380hold spaces. 381.It Oo Ad 2addr Oc Ns \\*(cMy\\*(dF/\\*(aRstring1\\*(dF/\\*(aRstring2\\*(dF/ 382Replaces all occurrences of collating 383elements in 384.Ar string1 385with the corresponding 386collating element in 387.Ar string2 . 388The lengths of 389.Ar string1 390and 391.Ar string2 392shall be equal. 393.It Oo Ad 2addr Oc Ns \\*(cM!\\*(dFfunction 394Applies the function (or group, if function 395is {) only to the lines that are not selected 396by the address(es). 397.It Oo Ad 0addr Oc Ns \\*(cM:\\*(dFlabel 398This command does nothing; it bears a label 399for the b and t commands to branch to. 400.It Oo Ad 1addr Oc Ns Cm \&= 401Places the current line number on the standard 402output as a line with its own line 403number. 404.It Oo Ad 0addr Oc 405An empty command is ignored. 406.It Oo Ad 0addr Oc Cm # 407If a 408.Cm # 409appears as the first character on any 410line of a script file, that entire line is 411ignored (treated as a comment), with the single 412exception that if the first line of the 413script file begins with 414.Cm Ns #n , 415the default 416output is suppressed. 417.El 418.Pp 419The 420.Nm sed 421utility exits 0 on success, and >0 if an error occurs. 422.Pp 423If one or more of the input (not script) files cannot be 424opened for reading, 425.Nm sed 426continues to process the remaining 427files. 428.Sh STANDARDS 429The 430.Nm sed 431utility is expected to be 432.St -p1003.2 433compatible. 434