[3613] | 1 | [NAME]
|
---|
| 2 | sed \- stream editor for filtering and transforming text
|
---|
| 3 | [SYNOPSIS]
|
---|
[599] | 4 | .nf
|
---|
| 5 | sed [-V] [--version] [--help] [-n] [--quiet] [--silent]
|
---|
| 6 | [-l N] [--line-length=N] [-u] [--unbuffered]
|
---|
[3613] | 7 | [-E] [-r] [--regexp-extended]
|
---|
[599] | 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
|
---|
[3613] | 45 | .B \-e
|
---|
[599] | 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
|
---|
[3613] | 70 | q [\fIexit-code\fR]
|
---|
[599] | 71 | Immediately quit the \*(sd script without processing
|
---|
[3613] | 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.
|
---|
[599] | 75 | .TP
|
---|
[3613] | 76 | Q [\fIexit-code\fR]
|
---|
[599] | 77 | Immediately quit the \*(sd script without processing
|
---|
[3613] | 78 | any more input. This is a GNU extension.
|
---|
[599] | 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 .
|
---|
[3613] | 87 | Each invocation of the command reads a line from the file.
|
---|
| 88 | This is a GNU extension.
|
---|
[599] | 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 | c \e
|
---|
| 103 | .TP
|
---|
| 104 | .I text
|
---|
| 105 | Replace the selected lines with
|
---|
| 106 | .IR text ,
|
---|
| 107 | which has each embedded newline preceded by a backslash.
|
---|
| 108 | .TP
|
---|
| 109 | d
|
---|
| 110 | Delete pattern space.
|
---|
| 111 | Start next cycle.
|
---|
| 112 | .TP
|
---|
| 113 | D
|
---|
[3613] | 114 | If pattern space contains no newline, start a normal new cycle as if
|
---|
| 115 | the d command was issued. Otherwise, delete text in the pattern
|
---|
| 116 | space up to the first newline, and restart cycle with the resultant
|
---|
| 117 | pattern space, without reading a new line of input.
|
---|
[599] | 118 | .TP
|
---|
| 119 | h H
|
---|
| 120 | Copy/append pattern space to hold space.
|
---|
| 121 | .TP
|
---|
| 122 | g G
|
---|
| 123 | Copy/append hold space to pattern space.
|
---|
| 124 | .TP
|
---|
| 125 | l
|
---|
| 126 | List out the current line in a ``visually unambiguous'' form.
|
---|
| 127 | .TP
|
---|
[3613] | 128 | .RI l\ width
|
---|
| 129 | List out the current line in a ``visually unambiguous'' form,
|
---|
| 130 | breaking it at
|
---|
| 131 | .I width
|
---|
| 132 | characters. This is a GNU extension.
|
---|
| 133 | .TP
|
---|
[599] | 134 | n N
|
---|
| 135 | Read/append the next line of input into the pattern space.
|
---|
| 136 | .TP
|
---|
| 137 | p
|
---|
| 138 | Print the current pattern space.
|
---|
| 139 | .TP
|
---|
| 140 | P
|
---|
| 141 | Print up to the first embedded newline of the current pattern space.
|
---|
| 142 | .TP
|
---|
| 143 | .RI s/ regexp / replacement /
|
---|
| 144 | Attempt to match
|
---|
| 145 | .I regexp
|
---|
| 146 | against the pattern space.
|
---|
| 147 | If successful, replace that portion matched
|
---|
| 148 | with
|
---|
| 149 | .IR replacement .
|
---|
| 150 | The
|
---|
| 151 | .I replacement
|
---|
| 152 | may contain the special character
|
---|
| 153 | .B &
|
---|
| 154 | to refer to that portion of the pattern space which matched,
|
---|
| 155 | and the special escapes \e1 through \e9 to refer to the
|
---|
| 156 | corresponding matching sub-expressions in the
|
---|
| 157 | .IR regexp .
|
---|
| 158 | .TP
|
---|
[3613] | 159 | .RI t\ label
|
---|
| 160 | If a s/// has done a successful substitution since the
|
---|
| 161 | last input line was read and since the last t or T
|
---|
| 162 | command, then branch to
|
---|
| 163 | .IR label ;
|
---|
| 164 | if
|
---|
| 165 | .I label
|
---|
| 166 | is omitted, branch to end of script.
|
---|
| 167 | .TP
|
---|
| 168 | .RI T\ label
|
---|
| 169 | If no s/// has done a successful substitution since the
|
---|
| 170 | last input line was read and since the last t or T
|
---|
| 171 | command, then branch to
|
---|
| 172 | .IR label ;
|
---|
| 173 | if
|
---|
| 174 | .I label
|
---|
| 175 | is omitted, branch to end of script. This is a GNU
|
---|
| 176 | extension.
|
---|
| 177 | .TP
|
---|
[599] | 178 | .RI w\ filename
|
---|
| 179 | Write the current pattern space to
|
---|
| 180 | .IR filename .
|
---|
| 181 | .TP
|
---|
| 182 | .RI W\ filename
|
---|
| 183 | Write the first line of the current pattern space to
|
---|
| 184 | .IR filename .
|
---|
[3613] | 185 | This is a GNU extension.
|
---|
[599] | 186 | .TP
|
---|
[3613] | 187 | x
|
---|
| 188 | Exchange the contents of the hold and pattern spaces.
|
---|
| 189 | .TP
|
---|
[599] | 190 | .RI y/ source / dest /
|
---|
| 191 | Transliterate the characters in the pattern space which appear in
|
---|
| 192 | .I source
|
---|
| 193 | to the corresponding character in
|
---|
| 194 | .IR dest .
|
---|
| 195 | .SH
|
---|
| 196 | Addresses
|
---|
| 197 | \*(Sd commands can be given with no addresses, in which
|
---|
| 198 | case the command will be executed for all input lines;
|
---|
| 199 | with one address, in which case the command will only be executed
|
---|
| 200 | for input lines which match that address; or with two
|
---|
| 201 | addresses, in which case the command will be executed
|
---|
| 202 | for all input lines which match the inclusive range of
|
---|
| 203 | lines starting from the first address and continuing to
|
---|
| 204 | the second address.
|
---|
| 205 | Three things to note about address ranges:
|
---|
| 206 | the syntax is
|
---|
| 207 | .IR addr1 , addr2
|
---|
| 208 | (i.e., the addresses are separated by a comma);
|
---|
| 209 | the line which
|
---|
| 210 | .I addr1
|
---|
| 211 | matched will always be accepted,
|
---|
| 212 | even if
|
---|
| 213 | .I addr2
|
---|
| 214 | selects an earlier line;
|
---|
| 215 | and if
|
---|
| 216 | .I addr2
|
---|
| 217 | is a
|
---|
| 218 | .IR regexp ,
|
---|
| 219 | it will not be tested against the line that
|
---|
| 220 | .I addr1
|
---|
| 221 | matched.
|
---|
| 222 | .PP
|
---|
| 223 | After the address (or address-range),
|
---|
| 224 | and before the command, a
|
---|
| 225 | .B !
|
---|
| 226 | may be inserted,
|
---|
| 227 | which specifies that the command shall only be
|
---|
| 228 | executed if the address (or address-range) does
|
---|
| 229 | .B not
|
---|
| 230 | match.
|
---|
| 231 | .PP
|
---|
| 232 | The following address types are supported:
|
---|
| 233 | .TP
|
---|
| 234 | .I number
|
---|
| 235 | Match only the specified line
|
---|
[3613] | 236 | .IR number
|
---|
| 237 | (which increments cumulatively across files, unless the
|
---|
| 238 | .B \-s
|
---|
| 239 | option is specified on the command line).
|
---|
[599] | 240 | .TP
|
---|
| 241 | .IR first ~ step
|
---|
| 242 | Match every
|
---|
| 243 | .IR step 'th
|
---|
| 244 | line starting with line
|
---|
| 245 | .IR first .
|
---|
[3613] | 246 | For example, ``sed \-n 1~2p'' will print all the odd-numbered lines in
|
---|
[599] | 247 | the input stream, and the address 2~5 will match every fifth line,
|
---|
[3613] | 248 | starting with the second.
|
---|
| 249 | .I first
|
---|
| 250 | can be zero; in this case, \*(sd operates as if it were equal to
|
---|
| 251 | .IR step .
|
---|
| 252 | (This is an extension.)
|
---|
[599] | 253 | .TP
|
---|
| 254 | $
|
---|
| 255 | Match the last line.
|
---|
| 256 | .TP
|
---|
| 257 | .RI / regexp /
|
---|
| 258 | Match lines matching the regular expression
|
---|
| 259 | .IR regexp .
|
---|
[3613] | 260 | Matching is performed on the current pattern space, which
|
---|
| 261 | can be modified with commands such as ``s///''.
|
---|
[599] | 262 | .TP
|
---|
| 263 | .BI \fR\e\fPc regexp c
|
---|
| 264 | Match lines matching the regular expression
|
---|
| 265 | .IR regexp .
|
---|
| 266 | The
|
---|
| 267 | .B c
|
---|
| 268 | may be any character.
|
---|
| 269 | .PP
|
---|
| 270 | GNU \*(sd also supports some special 2-address forms:
|
---|
| 271 | .TP
|
---|
| 272 | .RI 0, addr2
|
---|
| 273 | Start out in "matched first address" state, until
|
---|
| 274 | .I addr2
|
---|
| 275 | is found.
|
---|
| 276 | This is similar to
|
---|
| 277 | .RI 1, addr2 ,
|
---|
| 278 | except that if
|
---|
| 279 | .I addr2
|
---|
| 280 | matches the very first line of input the
|
---|
| 281 | .RI 0, addr2
|
---|
| 282 | form will be at the end of its range, whereas the
|
---|
| 283 | .RI 1, addr2
|
---|
| 284 | form will still be at the beginning of its range.
|
---|
[3613] | 285 | This works only when
|
---|
| 286 | .I addr2
|
---|
| 287 | is a regular expression.
|
---|
[599] | 288 | .TP
|
---|
| 289 | .IR addr1 ,+ N
|
---|
| 290 | Will match
|
---|
| 291 | .I addr1
|
---|
| 292 | and the
|
---|
| 293 | .I N
|
---|
| 294 | lines following
|
---|
| 295 | .IR addr1 .
|
---|
| 296 | .TP
|
---|
| 297 | .IR addr1 ,~ N
|
---|
| 298 | Will match
|
---|
| 299 | .I addr1
|
---|
| 300 | and the lines following
|
---|
| 301 | .I addr1
|
---|
| 302 | until the next line whose input line number is a multiple of
|
---|
| 303 | .IR N .
|
---|
| 304 |
|
---|
| 305 | [REGULAR EXPRESSIONS]
|
---|
| 306 | POSIX.2 BREs
|
---|
| 307 | .I should
|
---|
| 308 | be supported, but they aren't completely because of performance
|
---|
| 309 | problems.
|
---|
| 310 | The
|
---|
| 311 | .B \en
|
---|
| 312 | sequence in a regular expression matches the newline character,
|
---|
| 313 | and similarly for
|
---|
| 314 | .BR \ea ,
|
---|
| 315 | .BR \et ,
|
---|
| 316 | and other sequences.
|
---|
[3613] | 317 | The \fI-E\fP option switches to using extended regular expressions instead;
|
---|
| 318 | it has been supported for years by GNU sed, and is now
|
---|
| 319 | included in POSIX.
|
---|
[599] | 320 |
|
---|
| 321 | [SEE ALSO]
|
---|
| 322 | .BR awk (1),
|
---|
| 323 | .BR ed (1),
|
---|
| 324 | .BR grep (1),
|
---|
| 325 | .BR tr (1),
|
---|
| 326 | .BR perlre (1),
|
---|
| 327 | sed.info,
|
---|
| 328 | any of various books on \*(sd,
|
---|
| 329 | .na
|
---|
| 330 | the \*(sd FAQ (http://sed.sf.net/grabbag/tutorials/sedfaq.txt),
|
---|
| 331 | http://sed.sf.net/grabbag/.
|
---|
| 332 |
|
---|
| 333 | [BUGS]
|
---|
| 334 | .PP
|
---|
| 335 | E-mail bug reports to
|
---|
[3613] | 336 | .BR bug-sed@gnu.org .
|
---|
| 337 | Also, please include the output of ``sed \-\-version'' in the body
|
---|
[599] | 338 | of your report if at all possible.
|
---|