sed.1 (6954B)
1 .TH SED 1 2 .SH NAME 3 sed \- stream editor 4 .SH SYNOPSIS 5 .B sed 6 [ 7 .B -gln 8 ] 9 [ 10 .B -e 11 .I script 12 ] 13 [ 14 .B -f 15 .I sfile 16 ] 17 [ 18 .I file ... 19 ] 20 .SH DESCRIPTION 21 .I Sed 22 copies the named 23 .I files 24 (standard input default) to the standard output, 25 edited according to a script of commands. 26 The 27 .B -f 28 option causes the script to be taken from file 29 .IR sfile ; 30 these options accumulate. 31 If there is just one 32 .B -e 33 option and no 34 .BR -f 's, 35 the flag 36 .B -e 37 may be omitted. 38 The 39 .B -n 40 option suppresses the default output; 41 .B -g 42 causes all substitutions to be global, as if suffixed 43 .BR g . 44 The 45 .B -l 46 option causes 47 .I sed 48 to flush its output buffer after every newline. 49 .PP 50 A script consists of editing commands, one per line, 51 of the following form: 52 .IP 53 [\fIaddress\fR [\fL,\fI address\fR] ] \fIfunction\fR [\fIargument\fR ...] 54 .PP 55 In normal operation 56 .I sed 57 cyclically copies a line of input into a 58 .I pattern space 59 (unless there is something left after 60 a 61 .L D 62 command), 63 applies in sequence 64 all commands whose 65 .I addresses 66 select that pattern space, 67 and at the end of the script copies the pattern space 68 to the standard output (except under 69 .BR -n ) 70 and deletes the pattern space. 71 .PP 72 An 73 .I address 74 is either a decimal number that counts 75 input lines cumulatively across files, a 76 .L $ 77 that 78 addresses the last line of input, or a context address, 79 .BI / regular-expression / \f1, 80 in the style of 81 .IR regexp (7), 82 with the added convention that 83 .L \en 84 matches a 85 newline embedded in the pattern space. 86 .PP 87 A command line with no addresses selects every pattern space. 88 .PP 89 A command line with 90 one address selects each pattern space that matches the address. 91 .PP 92 A command line with 93 two addresses selects the inclusive range from the first 94 pattern space that matches the first address through 95 the next pattern space that matches 96 the second. 97 (If the second address is a number less than or equal 98 to the line number first selected, only one 99 line is selected.) 100 Thereafter the process is repeated, looking again for the 101 first address. 102 .PP 103 Editing commands can be applied to non-selected pattern 104 spaces by use of the negation function 105 .L ! 106 (below). 107 .PP 108 An argument denoted 109 .I text 110 consists of one or more lines, 111 all but the last of which end with 112 .L \e 113 to hide the 114 newline. 115 Backslashes in text are treated like backslashes 116 in the replacement string of an 117 .L s 118 command, 119 and may be used to protect initial blanks and tabs 120 against the stripping that is done on 121 every script line. 122 .PP 123 An argument denoted 124 .I rfile 125 or 126 .I wfile 127 must terminate the command 128 line and must be preceded by exactly one blank. 129 Each 130 .I wfile 131 is created before processing begins. 132 There can be at most 120 distinct 133 .I wfile 134 arguments. 135 .TP \w'\fL!\ \fIfunction\fLXXX'u 136 .B a\e 137 .br 138 .ns 139 .TP 140 .I text 141 Append. 142 Place 143 .I text 144 on the output before 145 reading the next input line. 146 .TP 147 .BI b " label" 148 Branch to the 149 .B : 150 command bearing the 151 .IR label . 152 If 153 .I label 154 is empty, branch to the end of the script. 155 .TP 156 .B c\e 157 .br 158 .ns 159 .TP 160 .I text 161 Change. 162 Delete the pattern space. 163 With 0 or 1 address or at the end of a 2-address range, place 164 .I text 165 on the output. 166 Start the next cycle. 167 .TP 168 .B d 169 Delete the pattern space. 170 Start the next cycle. 171 .TP 172 .B D 173 Delete the initial segment of the 174 pattern space through the first newline. 175 Start the next cycle. 176 .TP 177 .B g 178 Replace the contents of the pattern space 179 by the contents of the hold space. 180 .TP 181 .B G 182 Append the contents of the hold space to the pattern space. 183 .TP 184 .B h 185 Replace the contents of the hold space by the contents of the pattern space. 186 .TP 187 .B H 188 Append the contents of the pattern space to the hold space. 189 .ne 3 190 .TP 191 .B i\e 192 .br 193 .ns 194 .TP 195 .I text 196 Insert. 197 Place 198 .I text 199 on the standard output. 200 .TP 201 .B n 202 Copy the pattern space to the standard output. 203 Replace the pattern space with the next line of input. 204 .TP 205 .B N 206 Append the next line of input to the pattern space 207 with an embedded newline. 208 (The current line number changes.) 209 .TP 210 .B p 211 Print. 212 Copy the pattern space to the standard output. 213 .TP 214 .B P 215 Copy the initial segment of the pattern space through 216 the first newline to the standard output. 217 .TP 218 .B q 219 Quit. 220 Branch to the end of the script. 221 Do not start a new cycle. 222 .TP 223 .BI r " rfile" 224 Read the contents of 225 .IR rfile . 226 Place them on the output before reading 227 the next input line. 228 .TP 229 .B s/\fIregular-expression\fP/\fIreplacement\fP/\fIflags 230 Substitute the 231 .I replacement 232 string for instances of the 233 .I regular-expression 234 in the pattern space. 235 Any character may be used instead of 236 .LR / . 237 For a fuller description see 238 .IR regexp (7). 239 .I Flags 240 is zero or more of 241 .RS 242 .TP 243 .B g 244 Global. 245 Substitute for all non-overlapping instances of the 246 .I regular expression 247 rather than just the 248 first one. 249 .TP 250 .B p 251 Print the pattern space if a replacement was made. 252 .TP 253 .BI w " wfile" 254 Write. 255 Append the pattern space to 256 .I wfile 257 if a replacement 258 was made. 259 .RE 260 .TP 261 .BI t " label" 262 Test. 263 Branch to the 264 .L : 265 command bearing the 266 .I label 267 if any 268 substitutions have been made since the most recent 269 reading of an input line or execution of a 270 .LR t . 271 If 272 .I label 273 is empty, branch to the end of the script. 274 .TP 275 .B w 276 .I wfile 277 .br 278 Write. 279 Append the pattern space to 280 .IR wfile . 281 .TP 282 .B x 283 Exchange the contents of the pattern and hold spaces. 284 .TP 285 .B y/\fIstring1\fP/\fIstring2\fP/ 286 Transform. 287 Replace all occurrences of characters in 288 .I string1 289 with the corresponding character in 290 .IR string2 . 291 The lengths of 292 .I 293 string1 294 and 295 .I string2 296 must be equal. 297 .TP 298 .BI ! "function" 299 Don't. 300 Apply the 301 .I function 302 (or group, if 303 .I function 304 is 305 .LR { ) 306 only to lines 307 .I not 308 selected by the address(es). 309 .TP 310 .BI : " label" 311 This command does nothing; it bears a 312 .I label 313 for 314 .B b 315 and 316 .B t 317 commands to branch to. 318 .TP 319 .B = 320 Place the current line number on the standard output as a line. 321 .TP 322 .B { 323 Execute the following commands through a matching 324 .L } 325 only when the pattern space is selected. 326 .TP 327 .B " " 328 An empty command is ignored. 329 .ne 4 330 .SH EXAMPLES 331 .TP 332 .B sed 10q file 333 Print the first 10 lines of the file. 334 .TP 335 .B sed '/^$/d' 336 Delete empty lines from standard input. 337 .TP 338 .B sed 's/UNIX/& system/g' 339 Replace every instance of 340 .L UNIX 341 by 342 .LR "UNIX system" . 343 .PP 344 .EX 345 sed 's/ *$// \fRdrop trailing blanks\fP 346 /^$/d \fRdrop empty lines\fP 347 s/ */\e \fRreplace blanks by newlines\fP 348 /g 349 /^$/d' chapter* 350 .EE 351 .ns 352 .IP 353 Print the files 354 .BR chapter1 , 355 .BR chapter2 , 356 etc. one word to a line. 357 .PP 358 .EX 359 nroff -ms manuscript | sed ' 360 ${ 361 /^$/p \fRif last line of file is empty, print it\fP 362 } 363 //N \fRif current line is empty, append next line\fP 364 /^\en$/D' \fRif two lines are empty, delete the first\fP 365 .EE 366 .ns 367 .IP 368 Delete all but one of each group of empty lines from a 369 formatted manuscript. 370 .SH SOURCE 371 .B \*9/src/cmd/sed.c 372 .SH SEE ALSO 373 .IR ed (1), 374 .IR grep (1), 375 .IR awk (1), 376 .IR lex (1), 377 .IR sam (1), 378 .IR regexp (7) 379 .br 380 L. E. McMahon, 381 `SED \(em A Non-interactive Text Editor', 382 Unix Research System Programmer's Manual, Volume 2. 383 .SH BUGS 384 If input is from a pipe, buffering may consume 385 characters beyond a line on which a 386 .L q 387 command is executed.