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