1 .SH NAME 2 sed \- a Stream EDitor 3 .SH SYNOPSIS 4 .nf 5 sed [-V] [--version] [--help] [-n] [--quiet] [--silent] 6 [-l N] [--line-length=N] [-u] [--unbuffered] 7 [-r] [--regexp-extended] 8 [-e script] [--expression=script] 9 [-f script-file] [--file=script-file] 10 [script-if-no-other-script] 11 [file...] 12 .fi 13 [DESCRIPTION] 14 .ds sd \fIsed\fP 15 .ds Sd \fISed\fP 16 \*(Sd is a stream editor. 17 A stream editor is used to perform basic text 18 transformations on an input stream 19 (a file or input from a pipeline). 20 While in some ways similar to an editor which 21 permits scripted edits (such as \fIed\fP), 22 \*(sd works by making only one pass over the 23 input(s), and is consequently more efficient. 24 But it is \*(sd's ability to filter text in a pipeline 25 which particularly distinguishes it from other types of 26 editors. 27 28 [COMMAND SYNOPSIS] 29 This is just a brief synopsis of \*(sd commands to serve as 30 a reminder to those who already know \*(sd; 31 other documentation (such as the texinfo document) 32 must be consulted for fuller descriptions. 33 .SS 34 Zero-address ``commands'' 35 .TP 36 .RI :\ label 37 Label for 38 .B b 39 and 40 .B t 41 commands. 42 .TP 43 .RI # comment 44 The comment extends until the next newline (or the end of a 45 .B -e 46 script fragment). 47 .TP 48 } 49 The closing bracket of a { } block. 50 .SS 51 Zero- or One- address commands 52 .TP 53 = 54 Print the current line number. 55 .TP 56 a \e 57 .TP 58 .I text 59 Append 60 .IR text , 61 which has each embedded newline preceded by a backslash. 62 .TP 63 i \e 64 .TP 65 .I text 66 Insert 67 .IR text , 68 which has each embedded newline preceded by a backslash. 69 .TP 70 q [\fIexit-code\fR] 71 Immediately quit the \*(sd script without processing 72 any more input, except that if auto-print is not disabled 73 the current pattern space will be printed. The exit code 74 argument is a GNU extension. 75 .TP 76 Q [\fIexit-code\fR] 77 Immediately quit the \*(sd script without processing 78 any more input. This is a GNU extension. 79 .TP 80 .RI r\ filename 81 Append text read from 82 .IR filename . 83 .TP 84 .RI R\ filename 85 Append a line read from 86 .IR filename . 87 Each invocation of the command reads a line from the file. 88 This is a GNU extension. 89 .SS 90 Commands which accept address ranges 91 .TP 92 { 93 Begin a block of commands (end with a }). 94 .TP 95 .RI b\ label 96 Branch to 97 .IR label ; 98 if 99 .I label 100 is omitted, branch to end of script. 101 .TP 102 .RI t\ label 103 If a s/// has done a successful substitution since the 104 last input line was read and since the last t or T 105 command, then branch to 106 .IR label ; 107 if 108 .I label 109 is omitted, branch to end of script. 110 .TP 111 .RI T\ label 112 If no s/// has done a successful substitution since the 113 last input line was read and since the last t or T 114 command, then branch to 115 .IR label ; 116 if 117 .I label 118 is omitted, branch to end of script. This is a GNU 119 extension. 120 .TP 121 c \e 122 .TP 123 .I text 124 Replace the selected lines with 125 .IR text , 126 which has each embedded newline preceded by a backslash. 127 .TP 128 d 129 Delete pattern space. 130 Start next cycle. 131 .TP 132 D 133 Delete up to the first embedded newline in the pattern space. 134 Start next cycle, but skip reading from the input 135 if there is still data in the pattern space. 136 .TP 137 h H 138 Copy/append pattern space to hold space. 139 .TP 140 g G 141 Copy/append hold space to pattern space. 142 .TP 143 x 144 Exchange the contents of the hold and pattern spaces. 145 .TP 146 l 147 List out the current line in a ``visually unambiguous'' form. 148 .TP 149 .RI l\ width 150 List out the current line in a ``visually unambiguous'' form, 151 breaking it at 152 .I width 153 characters. This is a GNU extension. 154 .TP 155 n N 156 Read/append the next line of input into the pattern space. 157 .TP 158 p 159 Print the current pattern space. 160 .TP 161 P 162 Print up to the first embedded newline of the current pattern space. 163 .TP 164 .RI s/ regexp / replacement / 165 Attempt to match 166 .I regexp 167 against the pattern space. 168 If successful, replace that portion matched 169 with 170 .IR replacement . 171 The 172 .I replacement 173 may contain the special character 174 .B & 175 to refer to that portion of the pattern space which matched, 176 and the special escapes \e1 through \e9 to refer to the 177 corresponding matching sub-expressions in the 178 .IR regexp . 179 .TP 180 .RI w\ filename 181 Write the current pattern space to 182 .IR filename . 183 .TP 184 .RI W\ filename 185 Write the first line of the current pattern space to 186 .IR filename . 187 This is a GNU extension. 188 .TP 189 .RI y/ source / dest / 190 Transliterate the characters in the pattern space which appear in 191 .I source 192 to the corresponding character in 193 .IR dest . 194 .SH 195 Addresses 196 \*(Sd commands can be given with no addresses, in which 197 case the command will be executed for all input lines; 198 with one address, in which case the command will only be executed 199 for input lines which match that address; or with two 200 addresses, in which case the command will be executed 201 for all input lines which match the inclusive range of 202 lines starting from the first address and continuing to 203 the second address. 204 Three things to note about address ranges: 205 the syntax is 206 .IR addr1 , addr2 207 (i.e., the addresses are separated by a comma); 208 the line which 209 .I addr1 210 matched will always be accepted, 211 even if 212 .I addr2 213 selects an earlier line; 214 and if 215 .I addr2 216 is a 217 .IR regexp , 218 it will not be tested against the line that 219 .I addr1 220 matched. 221 .PP 222 After the address (or address-range), 223 and before the command, a 224 .B ! 225 may be inserted, 226 which specifies that the command shall only be 227 executed if the address (or address-range) does 228 .B not 229 match. 230 .PP 231 The following address types are supported: 232 .TP 233 .I number 234 Match only the specified line 235 .IR number . 236 .TP 237 .IR first ~ step 238 Match every 239 .IR step 'th 240 line starting with line 241 .IR first . 242 For example, ``sed -n 1~2p'' will print all the odd-numbered lines in 243 the input stream, and the address 2~5 will match every fifth line, 244 starting with the second. 245 .I first 246 can be zero; in this case, \*(sd operates as if it were equal to 247 .IR step . 248 (This is an extension.) 249 .TP 250 $ 251 Match the last line. 252 .TP 253 .RI / regexp / 254 Match lines matching the regular expression 255 .IR regexp . 256 .TP 257 .BI \fR\e\fPc regexp c 258 Match lines matching the regular expression 259 .IR regexp . 260 The 261 .B c 262 may be any character. 263 .PP 264 GNU \*(sd also supports some special 2-address forms: 265 .TP 266 .RI 0, addr2 267 Start out in "matched first address" state, until 268 .I addr2 269 is found. 270 This is similar to 271 .RI 1, addr2 , 272 except that if 273 .I addr2 274 matches the very first line of input the 275 .RI 0, addr2 276 form will be at the end of its range, whereas the 277 .RI 1, addr2 278 form will still be at the beginning of its range. 279 This works only when 280 .I addr2 281 is a regular expression. 282 .TP 283 .IR addr1 ,+ N 284 Will match 285 .I addr1 286 and the 287 .I N 288 lines following 289 .IR addr1 . 290 .TP 291 .IR addr1 ,~ N 292 Will match 293 .I addr1 294 and the lines following 295 .I addr1 296 until the next line whose input line number is a multiple of 297 .IR N . 298 299 [REGULAR EXPRESSIONS] 300 POSIX.2 BREs 301 .I should 302 be supported, but they aren't completely because of performance 303 problems. 304 The 305 .B \en 306 sequence in a regular expression matches the newline character, 307 and similarly for 308 .BR \ea , 309 .BR \et , 310 and other sequences. 311 312 [SEE ALSO] 313 .BR awk (1), 314 .BR ed (1), 315 .BR grep (1), 316 .BR tr (1), 317 .BR perlre (1), 318 sed.info, 319 any of various books on \*(sd, 320 .na 321 the \*(sd FAQ (http://sed.sf.net/grabbag/tutorials/sedfaq.txt), 322 http://sed.sf.net/grabbag/. 323 324 [BUGS] 325 .PP 326 E-mail bug reports to 327 .BR bonzini (at) gnu.org . 328 Be sure to include the word ``sed'' somewhere in the ``Subject:'' field. 329 Also, please include the output of ``sed --version'' in the body 330 of your report if at all possible. 331