| 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
|
|---|
| 71 | Immediately quit the \*(sd script without processing
|
|---|
| 72 | any more input,
|
|---|
| 73 | except that if auto-print is not disabled
|
|---|
| 74 | the current pattern space will be printed.
|
|---|
| 75 | .TP
|
|---|
| 76 | Q
|
|---|
| 77 | Immediately quit the \*(sd script without processing
|
|---|
| 78 | any more input.
|
|---|
| 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 | .SS
|
|---|
| 88 | Commands which accept address ranges
|
|---|
| 89 | .TP
|
|---|
| 90 | {
|
|---|
| 91 | Begin a block of commands (end with a }).
|
|---|
| 92 | .TP
|
|---|
| 93 | .RI b\ label
|
|---|
| 94 | Branch to
|
|---|
| 95 | .IR label ;
|
|---|
| 96 | if
|
|---|
| 97 | .I label
|
|---|
| 98 | is omitted, branch to end of script.
|
|---|
| 99 | .TP
|
|---|
| 100 | .RI t\ label
|
|---|
| 101 | If a s/// has done a successful substitution since the
|
|---|
| 102 | last input line was read and since the last t or T
|
|---|
| 103 | command, then branch to
|
|---|
| 104 | .IR label ;
|
|---|
| 105 | if
|
|---|
| 106 | .I label
|
|---|
| 107 | is omitted, branch to end of script.
|
|---|
| 108 | .TP
|
|---|
| 109 | .RI T\ label
|
|---|
| 110 | If no s/// has done a successful substitution since the
|
|---|
| 111 | last input line was read and since the last t or T
|
|---|
| 112 | command, then branch to
|
|---|
| 113 | .IR label ;
|
|---|
| 114 | if
|
|---|
| 115 | .I label
|
|---|
| 116 | is omitted, branch to end of script.
|
|---|
| 117 | .TP
|
|---|
| 118 | c \e
|
|---|
| 119 | .TP
|
|---|
| 120 | .I text
|
|---|
| 121 | Replace the selected lines with
|
|---|
| 122 | .IR text ,
|
|---|
| 123 | which has each embedded newline preceded by a backslash.
|
|---|
| 124 | .TP
|
|---|
| 125 | d
|
|---|
| 126 | Delete pattern space.
|
|---|
| 127 | Start next cycle.
|
|---|
| 128 | .TP
|
|---|
| 129 | D
|
|---|
| 130 | Delete up to the first embedded newline in the pattern space.
|
|---|
| 131 | Start next cycle, but skip reading from the input
|
|---|
| 132 | if there is still data in the pattern space.
|
|---|
| 133 | .TP
|
|---|
| 134 | h H
|
|---|
| 135 | Copy/append pattern space to hold space.
|
|---|
| 136 | .TP
|
|---|
| 137 | g G
|
|---|
| 138 | Copy/append hold space to pattern space.
|
|---|
| 139 | .TP
|
|---|
| 140 | x
|
|---|
| 141 | Exchange the contents of the hold and pattern spaces.
|
|---|
| 142 | .TP
|
|---|
| 143 | l
|
|---|
| 144 | List out the current line in a ``visually unambiguous'' form.
|
|---|
| 145 | .TP
|
|---|
| 146 | n N
|
|---|
| 147 | Read/append the next line of input into the pattern space.
|
|---|
| 148 | .TP
|
|---|
| 149 | p
|
|---|
| 150 | Print the current pattern space.
|
|---|
| 151 | .TP
|
|---|
| 152 | P
|
|---|
| 153 | Print up to the first embedded newline of the current pattern space.
|
|---|
| 154 | .TP
|
|---|
| 155 | .RI s/ regexp / replacement /
|
|---|
| 156 | Attempt to match
|
|---|
| 157 | .I regexp
|
|---|
| 158 | against the pattern space.
|
|---|
| 159 | If successful, replace that portion matched
|
|---|
| 160 | with
|
|---|
| 161 | .IR replacement .
|
|---|
| 162 | The
|
|---|
| 163 | .I replacement
|
|---|
| 164 | may contain the special character
|
|---|
| 165 | .B &
|
|---|
| 166 | to refer to that portion of the pattern space which matched,
|
|---|
| 167 | and the special escapes \e1 through \e9 to refer to the
|
|---|
| 168 | corresponding matching sub-expressions in the
|
|---|
| 169 | .IR regexp .
|
|---|
| 170 | .TP
|
|---|
| 171 | .RI w\ filename
|
|---|
| 172 | Write the current pattern space to
|
|---|
| 173 | .IR filename .
|
|---|
| 174 | .TP
|
|---|
| 175 | .RI W\ filename
|
|---|
| 176 | Write the first line of the current pattern space to
|
|---|
| 177 | .IR filename .
|
|---|
| 178 | .TP
|
|---|
| 179 | .RI y/ source / dest /
|
|---|
| 180 | Transliterate the characters in the pattern space which appear in
|
|---|
| 181 | .I source
|
|---|
| 182 | to the corresponding character in
|
|---|
| 183 | .IR dest .
|
|---|
| 184 | .SH
|
|---|
| 185 | Addresses
|
|---|
| 186 | \*(Sd commands can be given with no addresses, in which
|
|---|
| 187 | case the command will be executed for all input lines;
|
|---|
| 188 | with one address, in which case the command will only be executed
|
|---|
| 189 | for input lines which match that address; or with two
|
|---|
| 190 | addresses, in which case the command will be executed
|
|---|
| 191 | for all input lines which match the inclusive range of
|
|---|
| 192 | lines starting from the first address and continuing to
|
|---|
| 193 | the second address.
|
|---|
| 194 | Three things to note about address ranges:
|
|---|
| 195 | the syntax is
|
|---|
| 196 | .IR addr1 , addr2
|
|---|
| 197 | (i.e., the addresses are separated by a comma);
|
|---|
| 198 | the line which
|
|---|
| 199 | .I addr1
|
|---|
| 200 | matched will always be accepted,
|
|---|
| 201 | even if
|
|---|
| 202 | .I addr2
|
|---|
| 203 | selects an earlier line;
|
|---|
| 204 | and if
|
|---|
| 205 | .I addr2
|
|---|
| 206 | is a
|
|---|
| 207 | .IR regexp ,
|
|---|
| 208 | it will not be tested against the line that
|
|---|
| 209 | .I addr1
|
|---|
| 210 | matched.
|
|---|
| 211 | .PP
|
|---|
| 212 | After the address (or address-range),
|
|---|
| 213 | and before the command, a
|
|---|
| 214 | .B !
|
|---|
| 215 | may be inserted,
|
|---|
| 216 | which specifies that the command shall only be
|
|---|
| 217 | executed if the address (or address-range) does
|
|---|
| 218 | .B not
|
|---|
| 219 | match.
|
|---|
| 220 | .PP
|
|---|
| 221 | The following address types are supported:
|
|---|
| 222 | .TP
|
|---|
| 223 | .I number
|
|---|
| 224 | Match only the specified line
|
|---|
| 225 | .IR number .
|
|---|
| 226 | .TP
|
|---|
| 227 | .IR first ~ step
|
|---|
| 228 | Match every
|
|---|
| 229 | .IR step 'th
|
|---|
| 230 | line starting with line
|
|---|
| 231 | .IR first .
|
|---|
| 232 | For example, ``sed -n 1~2p'' will print all the odd-numbered lines in
|
|---|
| 233 | the input stream, and the address 2~5 will match every fifth line,
|
|---|
| 234 | starting with the second. (This is an extension.)
|
|---|
| 235 | .TP
|
|---|
| 236 | $
|
|---|
| 237 | Match the last line.
|
|---|
| 238 | .TP
|
|---|
| 239 | .RI / regexp /
|
|---|
| 240 | Match lines matching the regular expression
|
|---|
| 241 | .IR regexp .
|
|---|
| 242 | .TP
|
|---|
| 243 | .BI \fR\e\fPc regexp c
|
|---|
| 244 | Match lines matching the regular expression
|
|---|
| 245 | .IR regexp .
|
|---|
| 246 | The
|
|---|
| 247 | .B c
|
|---|
| 248 | may be any character.
|
|---|
| 249 | .PP
|
|---|
| 250 | GNU \*(sd also supports some special 2-address forms:
|
|---|
| 251 | .TP
|
|---|
| 252 | .RI 0, addr2
|
|---|
| 253 | Start out in "matched first address" state, until
|
|---|
| 254 | .I addr2
|
|---|
| 255 | is found.
|
|---|
| 256 | This is similar to
|
|---|
| 257 | .RI 1, addr2 ,
|
|---|
| 258 | except that if
|
|---|
| 259 | .I addr2
|
|---|
| 260 | matches the very first line of input the
|
|---|
| 261 | .RI 0, addr2
|
|---|
| 262 | form will be at the end of its range, whereas the
|
|---|
| 263 | .RI 1, addr2
|
|---|
| 264 | form will still be at the beginning of its range.
|
|---|
| 265 | .TP
|
|---|
| 266 | .IR addr1 ,+ N
|
|---|
| 267 | Will match
|
|---|
| 268 | .I addr1
|
|---|
| 269 | and the
|
|---|
| 270 | .I N
|
|---|
| 271 | lines following
|
|---|
| 272 | .IR addr1 .
|
|---|
| 273 | .TP
|
|---|
| 274 | .IR addr1 ,~ N
|
|---|
| 275 | Will match
|
|---|
| 276 | .I addr1
|
|---|
| 277 | and the lines following
|
|---|
| 278 | .I addr1
|
|---|
| 279 | until the next line whose input line number is a multiple of
|
|---|
| 280 | .IR N .
|
|---|
| 281 |
|
|---|
| 282 | [REGULAR EXPRESSIONS]
|
|---|
| 283 | POSIX.2 BREs
|
|---|
| 284 | .I should
|
|---|
| 285 | be supported, but they aren't completely because of performance
|
|---|
| 286 | problems.
|
|---|
| 287 | The
|
|---|
| 288 | .B \en
|
|---|
| 289 | sequence in a regular expression matches the newline character,
|
|---|
| 290 | and similarly for
|
|---|
| 291 | .BR \ea ,
|
|---|
| 292 | .BR \et ,
|
|---|
| 293 | and other sequences.
|
|---|
| 294 |
|
|---|
| 295 | [SEE ALSO]
|
|---|
| 296 | .BR awk (1),
|
|---|
| 297 | .BR ed (1),
|
|---|
| 298 | .BR grep (1),
|
|---|
| 299 | .BR tr (1),
|
|---|
| 300 | .BR perlre (1),
|
|---|
| 301 | sed.info,
|
|---|
| 302 | any of various books on \*(sd,
|
|---|
| 303 | .na
|
|---|
| 304 | the \*(sd FAQ (http://sed.sf.net/grabbag/tutorials/sedfaq.txt),
|
|---|
| 305 | http://sed.sf.net/grabbag/.
|
|---|
| 306 |
|
|---|
| 307 | [BUGS]
|
|---|
| 308 | .PP
|
|---|
| 309 | E-mail bug reports to
|
|---|
| 310 | .BR bonzini@gnu.org .
|
|---|
| 311 | Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
|
|---|
| 312 | Also, please include the output of ``sed --version'' in the body
|
|---|
| 313 | of your report if at all possible.
|
|---|