[9] | 1 | .TH sed 1 "November 19, 1995"
|
---|
| 2 | .SH NAME
|
---|
| 3 | sed \- the stream editor
|
---|
| 4 | .SH SYNOPSIS
|
---|
| 5 | .B sed
|
---|
| 6 | .RB [ \-n ]
|
---|
| 7 | .RB [ \-g ]
|
---|
| 8 | .RB [ \-e
|
---|
| 9 | .IR script ]
|
---|
| 10 | .RB [ \-f
|
---|
| 11 | .IR sfile ]
|
---|
| 12 | .RI [ file " ...]"
|
---|
| 13 | .SH DESCRIPTION
|
---|
| 14 | Sed copies the named files (standard input default) to the standard
|
---|
| 15 | output, edited according to a script of commands.
|
---|
| 16 | .P
|
---|
| 17 | An
|
---|
| 18 | .B \-e
|
---|
| 19 | option supplies a single edit command from the next argument;
|
---|
| 20 | if there are several of these they are executed in the order in which
|
---|
| 21 | they appear. If there is just one
|
---|
| 22 | .B \-e
|
---|
| 23 | option and no
|
---|
| 24 | .BR \-f "'s,"
|
---|
| 25 | the
|
---|
| 26 | .B \-e
|
---|
| 27 | flag may be omitted.
|
---|
| 28 | .P
|
---|
| 29 | An
|
---|
| 30 | .B \-f
|
---|
| 31 | option causes commands to be taken from the file "sfile"; if
|
---|
| 32 | there are several of these they are executed in the order in which
|
---|
| 33 | they appear;
|
---|
| 34 | .B \-e
|
---|
| 35 | and
|
---|
| 36 | .B \-f
|
---|
| 37 | commands may be mixed.
|
---|
| 38 | .P
|
---|
| 39 | The
|
---|
| 40 | .B \-g
|
---|
| 41 | option causes
|
---|
| 42 | .B sed
|
---|
| 43 | to act as though every substitute command
|
---|
| 44 | in the script has a
|
---|
| 45 | .B g
|
---|
| 46 | suffix.
|
---|
| 47 | .P
|
---|
| 48 | The
|
---|
| 49 | .B \-n
|
---|
| 50 | option suppresses the default output.
|
---|
| 51 | .P
|
---|
| 52 | A script consists of commands, one per line, of the following form:
|
---|
| 53 | .PP
|
---|
| 54 | [address [, address] ] function [arguments]
|
---|
| 55 | .PP
|
---|
| 56 | Normally
|
---|
| 57 | .B sed
|
---|
| 58 | cyclically copies a line of input into a current text
|
---|
| 59 | buffer, then applies all commands whose addresses select the buffer in
|
---|
| 60 | sequence, then copies the buffer to standard output and clears it.
|
---|
| 61 | .P
|
---|
| 62 | The
|
---|
| 63 | .B \-n
|
---|
| 64 | option suppresses normal output (so that only
|
---|
| 65 | .B p
|
---|
| 66 | and
|
---|
| 67 | .B w
|
---|
| 68 | output is done). Also, some commands
|
---|
| 69 | .RB ( n ,
|
---|
| 70 | .BR N )
|
---|
| 71 | do their own line reads, and some others
|
---|
| 72 | .RB ( d ,
|
---|
| 73 | .BR D )
|
---|
| 74 | cause all commands following in the script to be skipped (the
|
---|
| 75 | .B D
|
---|
| 76 | command also suppresses the clearing of the current text
|
---|
| 77 | buffer that would normally occur before the next cycle).
|
---|
| 78 | .P
|
---|
| 79 | It is also helpful to know that there's a second buffer (called the `hold
|
---|
| 80 | space' that can be copied or appended to or from or swapped with
|
---|
| 81 | the current text buffer.
|
---|
| 82 | .P
|
---|
| 83 | An address is: a decimal numeral (which matches the line it numbers where line
|
---|
| 84 | numbers start at 1 and run cumulatively across files), or a `$' that addresses
|
---|
| 85 | the last line of input, or a context address, which is a `/regular
|
---|
| 86 | expression/', in the style of
|
---|
| 87 | .BR ed (1)
|
---|
| 88 | modified thus:
|
---|
| 89 | .P
|
---|
| 90 | .TP 5
|
---|
| 91 | (1)
|
---|
| 92 | The escape sequence `\en' matches a newline embedded in the buffer,
|
---|
| 93 | and `\et' matches a tab.
|
---|
| 94 | .TP 5
|
---|
| 95 | (2)
|
---|
| 96 | A command line with no addresses selects every buffer.
|
---|
| 97 | .TP 5
|
---|
| 98 | (3)
|
---|
| 99 | A command line with one address selects every buffer that matches
|
---|
| 100 | that address.
|
---|
| 101 | .TP 5
|
---|
| 102 | (4)
|
---|
| 103 | A command line with two addresses selects the inclusive range from
|
---|
| 104 | the first input buffer that matches the first address through the
|
---|
| 105 | next input buffer that matches the second. (If the second address
|
---|
| 106 | is a number less than or equal to the line number first selected,
|
---|
| 107 | only one line is selected.) Once the second address is matched
|
---|
| 108 | .B sed
|
---|
| 109 | starts looking for the first one again; thus, any number of these
|
---|
| 110 | ranges will be matched.
|
---|
| 111 | .P
|
---|
| 112 | The negation operator '!' can prefix a command to apply it to every
|
---|
| 113 | line not selected by the address(es).
|
---|
| 114 | .P
|
---|
| 115 | In the following list of functions, the maximum number of addresses
|
---|
| 116 | permitted for each function is indicated in parentheses.
|
---|
| 117 | .P
|
---|
| 118 | An argument denoted "text" consists of one or more lines, with all
|
---|
| 119 | but the last ending with `\e' to hide the newline.
|
---|
| 120 | .P
|
---|
| 121 | Backslashes in text are treated like backslashes in the replacement
|
---|
| 122 | string of an
|
---|
| 123 | .B s
|
---|
| 124 | command and may be used to protect initial whitespace (blanks and tabs)
|
---|
| 125 | against the stripping that is done on every line of the script.
|
---|
| 126 | .P
|
---|
| 127 | An argument denoted "rfile" or "wfile" must be last on the command
|
---|
| 128 | line. Each wfile is created before processing begins. There can be at
|
---|
| 129 | most 10 distinct wfile arguments.
|
---|
| 130 | .ta +\w'nm'u +\w'"command"m'u
|
---|
| 131 | .TP 5
|
---|
| 132 | a "text" (1)
|
---|
| 133 | Append. Place text on output before reading the next input line.
|
---|
| 134 | .TP 5
|
---|
| 135 | b "label" (2)
|
---|
| 136 | Branch to the `:' command bearing the label. If no label is given,
|
---|
| 137 | branch to the end of the script.
|
---|
| 138 | .TP 5
|
---|
| 139 | c "text" (2)
|
---|
| 140 | Change. Delete the current text buffer. With 0 or 1 address, or at
|
---|
| 141 | the end of a 2-address range, place text on the output. Start the next
|
---|
| 142 | cycle.
|
---|
| 143 | .TP 5
|
---|
| 144 | d (2)
|
---|
| 145 | Delete the current text buffer. Start the next cycle.
|
---|
| 146 | .TP 5
|
---|
| 147 | D (2)
|
---|
| 148 | Delete the first line of the current text buffer (all chars up to the
|
---|
| 149 | first newline). Start the next cycle.
|
---|
| 150 | .TP 5
|
---|
| 151 | g (2)
|
---|
| 152 | Replace the contents of the current text buffer with the contents of
|
---|
| 153 | the hold space.
|
---|
| 154 | .TP 5
|
---|
| 155 | G (2)
|
---|
| 156 | Append the contents of the hold space to the current text buffer.
|
---|
| 157 | .TP 5
|
---|
| 158 | h (2)
|
---|
| 159 | Copy the current text buffer into the hold space.
|
---|
| 160 | .TP 5
|
---|
| 161 | H (2)
|
---|
| 162 | Append a copy of the current text buffer to the hold space.
|
---|
| 163 | .TP 5
|
---|
| 164 | i "text" (1)
|
---|
| 165 | Insert. Place text on the standard output.
|
---|
| 166 | .TP 5
|
---|
| 167 | l (2)
|
---|
| 168 | List. Sends the pattern space to standard output. A "w" option may
|
---|
| 169 | follow as in the
|
---|
| 170 | .B s
|
---|
| 171 | command below. Non-printable characters expand to:
|
---|
| 172 | .sp .4v
|
---|
| 173 | .in +3
|
---|
| 174 | .nf
|
---|
| 175 | .ta +\w'xxxn'u +\w'nnnn'u +\w'backspace 'u
|
---|
| 176 | \eb \-\- backspace (ASCII 08)
|
---|
| 177 | \et \-\- tab (ASCII 09)
|
---|
| 178 | \en \-\- newline (ASCII 10)
|
---|
| 179 | \er \-\- return (ASCII 13)
|
---|
| 180 | \ee \-\- escape (ASCII 27)
|
---|
| 181 | \exx \-\- the ASCII character corresponding to 2 hex digits xx.
|
---|
| 182 | .fi
|
---|
| 183 | .in -3
|
---|
| 184 | .ta +\w'nm'u +\w'"command"m'u
|
---|
| 185 | .TP 5
|
---|
| 186 | n (2)
|
---|
| 187 | Copy the current text buffer to standard output. Read the next line
|
---|
| 188 | of input into it.
|
---|
| 189 | .TP 5
|
---|
| 190 | N (2)
|
---|
| 191 | Append the next line of input to the current text buffer, inserting
|
---|
| 192 | an embedded newline between the two. The current line number changes.
|
---|
| 193 | .TP 5
|
---|
| 194 | p (2)
|
---|
| 195 | Print. Copy the current text buffer to the standard output.
|
---|
| 196 | .TP 5
|
---|
| 197 | P (2)
|
---|
| 198 | Copy the first line of the current text buffer (all chars up to the
|
---|
| 199 | first newline) to standard output.
|
---|
| 200 | .TP 5
|
---|
| 201 | q (1)
|
---|
| 202 | Quit. Branch to the end of the script. Do not start a new cycle.
|
---|
| 203 | .TP 5
|
---|
| 204 | r "rfile" (1)
|
---|
| 205 | Read the contents of rfile. Place them on the output before reading
|
---|
| 206 | the next input line.
|
---|
| 207 | .TP 5
|
---|
| 208 | s /regular-expression/replacement/flags\0\0\0\0\0\0(2)
|
---|
| 209 | Substitute the replacement for instances of the regular expression
|
---|
| 210 | in the current text buffer. Any character may be used instead of `/'.
|
---|
| 211 | For a fuller description see ed (1).
|
---|
| 212 | Flags is zero or more of the following:
|
---|
| 213 | .sp .4v
|
---|
| 214 | .ta +\w'gm'u +\w'nnm'u
|
---|
| 215 | .in +\w'gmnnm'u
|
---|
| 216 | .ti -\w'gmnnm'u
|
---|
| 217 | g \-\- Global. Substitute for all nonoverlapping instances of
|
---|
| 218 | the string rather than just the first one.
|
---|
| 219 | .sp .4v
|
---|
| 220 | .ti -\w'gmnnm'u
|
---|
| 221 | p \-\- Print the pattern space if a replacement was made.
|
---|
| 222 | .sp .4v
|
---|
| 223 | .ti -\w'gmnnm'u
|
---|
| 224 | w \-\- Write. Append the current text buffer to a file argument
|
---|
| 225 | as in a w command if a replacement is made. Standard output is used if no
|
---|
| 226 | file argument is given
|
---|
| 227 | .in -\w'gmnnm'u
|
---|
| 228 | .ta +\w'nm'u +\w'"command"m'u
|
---|
| 229 | .TP 5
|
---|
| 230 | t "label" (2)
|
---|
| 231 | Branch-if-test. Branch to the
|
---|
| 232 | .B :
|
---|
| 233 | command with the given label if any
|
---|
| 234 | substitutes have been made since the most recent read of an input line
|
---|
| 235 | or execution of a
|
---|
| 236 | .B t
|
---|
| 237 | or
|
---|
| 238 | .BR T .
|
---|
| 239 | If no label is given, branch to the end of the script.
|
---|
| 240 | .TP 5
|
---|
| 241 | T "label" (2)
|
---|
| 242 | Branch-on-error. Branch to the
|
---|
| 243 | .B :
|
---|
| 244 | command with the given label if no substitutes have succeeded since the
|
---|
| 245 | last input line or
|
---|
| 246 | .B t
|
---|
| 247 | or
|
---|
| 248 | .B T
|
---|
| 249 | command. Branch to the end of the script if no label is given.
|
---|
| 250 | .TP 5
|
---|
| 251 | w "wfile" (2)
|
---|
| 252 | Write. Append the current text buffer to wfile.
|
---|
| 253 | .TP 5
|
---|
| 254 | W "wfile" (2)
|
---|
| 255 | Write first line. Append first line of the current text buffer
|
---|
| 256 | to wfile.
|
---|
| 257 | .TP 5
|
---|
| 258 | x (2)
|
---|
| 259 | Exchange the contents of the current text buffer and hold space.
|
---|
| 260 | .TP 5
|
---|
| 261 | y /string1/string2/\0\0\0\0\0\0(2)
|
---|
| 262 | Translate. Replace each occurrence of a character in string1 with
|
---|
| 263 | the corresponding character in string2. The lengths of these strings
|
---|
| 264 | must be equal.
|
---|
| 265 | .TP 5
|
---|
| 266 | ! "command" (2)
|
---|
| 267 | All-but. Apply the function (or group, if function is
|
---|
| 268 | .BR { )
|
---|
| 269 | only to lines not selected by the address(es).
|
---|
| 270 | .TP 5
|
---|
| 271 | : "label" (0)
|
---|
| 272 | This command does nothing but hold a label for
|
---|
| 273 | .B b
|
---|
| 274 | and
|
---|
| 275 | .B t
|
---|
| 276 | commands to branch to.
|
---|
| 277 | .TP 5
|
---|
| 278 | = (1)
|
---|
| 279 | Place the current line number on the standard output as a line.
|
---|
| 280 | .TP 5
|
---|
| 281 | { (2)
|
---|
| 282 | Execute the following commands through a matching `}' only when the
|
---|
| 283 | current line matches the address or address range given.
|
---|
| 284 | .P
|
---|
| 285 | An empty command is ignored.
|
---|
| 286 | .P
|
---|
| 287 | .SH PORTABILITY
|
---|
| 288 | This tool was reverse-engineered from BSD 4.1 UNIX
|
---|
| 289 | .BR sed ,
|
---|
| 290 | and (as far
|
---|
| 291 | as the author's knowledge and tests can determine) is compatible with
|
---|
| 292 | it. All documented features of BSD 4.1 sed are supported.
|
---|
| 293 | .P
|
---|
| 294 | One undocumented feature (a leading 'n' in the first comment having
|
---|
| 295 | the same effect as an
|
---|
| 296 | .B \-n
|
---|
| 297 | command-line option) has been omitted.
|
---|
| 298 | .P
|
---|
| 299 | The following bugs and limitations have been fixed:
|
---|
| 300 | .TP 5
|
---|
| 301 | \(bu
|
---|
| 302 | There is no hidden length limit (40 in BSD sed) on
|
---|
| 303 | .B w
|
---|
| 304 | file names.
|
---|
| 305 | .TP 5
|
---|
| 306 | \(bu
|
---|
| 307 | There is no limit (8 in BSD sed) on the length of labels.
|
---|
| 308 | .TP 5
|
---|
| 309 | \(bu
|
---|
| 310 | The exchange command now works for long pattern and hold spaces.
|
---|
| 311 | .P
|
---|
| 312 | The following enhancements to existing commands have been made:
|
---|
| 313 | .TP 5
|
---|
| 314 | \(bu
|
---|
| 315 | .BR a ,
|
---|
| 316 | .B i
|
---|
| 317 | commands don't insist on a leading backslash-\en in the text.
|
---|
| 318 | .TP 5
|
---|
| 319 | \(bu
|
---|
| 320 | .BR r ,
|
---|
| 321 | .B w
|
---|
| 322 | commands don't insist on whitespace before the filename.
|
---|
| 323 | .TP 5
|
---|
| 324 | \(bu
|
---|
| 325 | The
|
---|
| 326 | .BR g ,
|
---|
| 327 | .B p
|
---|
| 328 | and
|
---|
| 329 | .B P
|
---|
| 330 | options on
|
---|
| 331 | .B s
|
---|
| 332 | commands may be given in any order.
|
---|
| 333 | .P
|
---|
| 334 | Some enhancements to regular-expression syntax have been made:
|
---|
| 335 | .TP 5
|
---|
| 336 | \(bu
|
---|
| 337 | \et is recognized in REs (and elsewhere) as an escape for tab.
|
---|
| 338 | .TP 5
|
---|
| 339 | \(bu
|
---|
| 340 | In an RE, + calls for 1..n repeats of the previous pattern.
|
---|
| 341 | .P
|
---|
| 342 | The following are completely new features:
|
---|
| 343 | .TP 5
|
---|
| 344 | \(bu
|
---|
| 345 | The
|
---|
| 346 | .B l
|
---|
| 347 | command (list, undocumented and weaker in BSD)
|
---|
| 348 | .TP 5
|
---|
| 349 | \(bu
|
---|
| 350 | The
|
---|
| 351 | .B W
|
---|
| 352 | command (write first line of pattern space to file).
|
---|
| 353 | .TP 5
|
---|
| 354 | \(bu
|
---|
| 355 | The
|
---|
| 356 | .B T
|
---|
| 357 | command (branch on last substitute failed).
|
---|
| 358 | .TP 5
|
---|
| 359 | \(bu
|
---|
| 360 | Trailing comments are now allowed on command lines.
|
---|
| 361 | .P
|
---|
| 362 | In addition,
|
---|
| 363 | .BR sed "'s"
|
---|
| 364 | error messages have been made more specific and informative.
|
---|
| 365 | .P
|
---|
| 366 | The implementation is also significantly smaller and faster than
|
---|
| 367 | BSD 4.1 sed. It uses only the standard I/O library and exit(3).
|
---|
| 368 | .P
|
---|
| 369 | .SH NOTE
|
---|
| 370 | .P
|
---|
| 371 | This is a freeware component of the GNU and MINIX operating systems.
|
---|
| 372 | The user is hereby granted permission to use, modify, reproduce and
|
---|
| 373 | distribute it subject to the following conditions:
|
---|
| 374 | .P
|
---|
| 375 | 1. The authorship notice appearing in each source file may not be
|
---|
| 376 | altered or deleted.
|
---|
| 377 | .P
|
---|
| 378 | 2. The object form may not be distributed without source.
|
---|
| 379 | .P
|
---|
| 380 | .SH SEE ALSO
|
---|
| 381 | .P
|
---|
| 382 | .BR cgrep (1),
|
---|
| 383 | .BR fgrep (1),
|
---|
| 384 | .BR grep (1),
|
---|
| 385 | .BR lex (1),
|
---|
| 386 | .BR regexp (5),
|
---|
| 387 | .BR awk (9).
|
---|
| 388 | .P
|
---|
| 389 | .SH AUTHOR
|
---|
| 390 | Eric S. Raymond <esr@snark.thyrsus.com>
|
---|