fatbase

portable OpenBSD tools
git clone git://git.2f30.org/fatbase
Log | Files | Refs

sed.1 (15665B)


      1 .\"	$OpenBSD: sed.1,v 1.44 2014/10/22 23:23:22 schwarze Exp $
      2 .\"
      3 .\" Copyright (c) 1992, 1993
      4 .\"	The Regents of the University of California.  All rights reserved.
      5 .\"
      6 .\" This code is derived from software contributed to Berkeley by
      7 .\" the Institute of Electrical and Electronics Engineers, Inc.
      8 .\"
      9 .\" Redistribution and use in source and binary forms, with or without
     10 .\" modification, are permitted provided that the following conditions
     11 .\" are met:
     12 .\" 1. Redistributions of source code must retain the above copyright
     13 .\"    notice, this list of conditions and the following disclaimer.
     14 .\" 2. Redistributions in binary form must reproduce the above copyright
     15 .\"    notice, this list of conditions and the following disclaimer in the
     16 .\"    documentation and/or other materials provided with the distribution.
     17 .\" 3. Neither the name of the University nor the names of its contributors
     18 .\"    may be used to endorse or promote products derived from this software
     19 .\"    without specific prior written permission.
     20 .\"
     21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31 .\" SUCH DAMAGE.
     32 .\"
     33 .\"	from: @(#)sed.1	8.2 (Berkeley) 12/30/93
     34 .\"
     35 .Dd $Mdocdate: October 22 2014 $
     36 .Dt SED 1
     37 .Os
     38 .Sh NAME
     39 .Nm sed
     40 .Nd stream editor
     41 .Sh SYNOPSIS
     42 .Nm sed
     43 .Op Fl aEnru
     44 .Ar command
     45 .Op Ar
     46 .Nm sed
     47 .Op Fl aEnru
     48 .Op Fl e Ar command
     49 .Op Fl f Ar command_file
     50 .Op Ar
     51 .Sh DESCRIPTION
     52 The
     53 .Nm
     54 utility reads the specified files, or the standard input if no files
     55 are specified, modifying the input as specified by a list of commands.
     56 The input is then written to the standard output.
     57 .Pp
     58 A single command may be specified as the first argument to
     59 .Nm sed .
     60 Multiple commands may be specified
     61 separated by newlines or semicolons,
     62 or by using the
     63 .Fl e
     64 or
     65 .Fl f
     66 options.
     67 All commands are applied to the input in the order they are specified
     68 regardless of their origin.
     69 .Pp
     70 The options are as follows:
     71 .Bl -tag -width Ds
     72 .It Fl a
     73 The files listed as parameters for the
     74 .Ic w
     75 function or flag are created (or truncated) before any processing begins,
     76 by default.
     77 The
     78 .Fl a
     79 option causes
     80 .Nm
     81 to delay opening each file until a command containing the related
     82 .Ic w
     83 function or flag is applied to a line of input.
     84 .It Fl E
     85 Interpret regular expressions using POSIX extended regular expression syntax.
     86 The default behaviour is to use POSIX basic regular expression syntax.
     87 .It Fl e Ar command
     88 Append the editing commands specified by the
     89 .Ar command
     90 argument
     91 to the list of commands.
     92 .It Fl f Ar command_file
     93 Append the editing commands found in the file
     94 .Ar command_file
     95 to the list of commands.
     96 The editing commands should each be listed on a separate line.
     97 .It Fl r
     98 An alias for
     99 .Fl E ,
    100 for compatibility with GNU sed.
    101 .It Fl n
    102 By default, each line of input is echoed to the standard output after
    103 all of the commands have been applied to it.
    104 The
    105 .Fl n
    106 option suppresses this behavior.
    107 .It Fl u
    108 Force output to be line buffered,
    109 printing each line as it becomes available.
    110 By default, output is line buffered when standard output is a terminal
    111 and block buffered otherwise.
    112 See
    113 .Xr setbuf 3
    114 for a more detailed explanation.
    115 .El
    116 .Pp
    117 The form of a
    118 .Nm
    119 command is as follows:
    120 .Pp
    121 .Dl [address[,address]]function[arguments]
    122 .Pp
    123 Whitespace may be inserted before the first address and the function
    124 portions of the command.
    125 .Pp
    126 Normally,
    127 .Nm
    128 cyclically copies a line of input, not including its terminating newline
    129 character, into a
    130 .Em pattern space ,
    131 (unless there is something left after a
    132 .Ic D
    133 function),
    134 applies all of the commands with addresses that select that pattern space,
    135 copies the pattern space to the standard output, appending a newline, and
    136 deletes the pattern space.
    137 .Pp
    138 Some of the functions use a
    139 .Em hold space
    140 to save all or part of the pattern space for subsequent retrieval.
    141 .Sh SED ADDRESSES
    142 An address is not required, but if specified must be a number (that counts
    143 input lines
    144 cumulatively across input files), a dollar character
    145 .Pq Ql $
    146 that addresses the last line of input, or a context address
    147 (which consists of a regular expression preceded and followed by a
    148 delimiter).
    149 .Pp
    150 A command line with no addresses selects every pattern space.
    151 .Pp
    152 A command line with one address selects all of the pattern spaces
    153 that match the address.
    154 .Pp
    155 A command line with two addresses selects the inclusive range from
    156 the first pattern space that matches the first address through the next
    157 pattern space that matches the second.
    158 (If the second address is a number less than or equal to the line number
    159 first selected, only that line is selected.)
    160 Starting at the first line following the selected range,
    161 .Nm
    162 starts looking again for the first address.
    163 .Pp
    164 Editing commands can be applied to non-selected pattern spaces by use
    165 of the exclamation character
    166 .Pq Ql \&!
    167 function.
    168 .Sh SED REGULAR EXPRESSIONS
    169 By default,
    170 .Nm
    171 regular expressions are basic regular expressions
    172 .Pq BREs .
    173 Extended regular expressions are supported using the
    174 .Fl E
    175 and
    176 .Fl r
    177 options.
    178 See
    179 .Xr re_format 7
    180 for more information on regular expressions.
    181 In addition,
    182 .Nm
    183 has the following two additions to BREs:
    184 .Pp
    185 .Bl -enum -compact
    186 .It
    187 In a context address, any character other than a backslash
    188 .Pq Ql \e
    189 or newline character may be used to delimit the regular expression.
    190 The opening delimiter should be preceded by a backslash
    191 unless it is a slash.
    192 Putting a backslash character before the delimiting character
    193 causes the character to be treated literally.
    194 For example, in the context address \exabc\exdefx, the RE delimiter
    195 is an
    196 .Sq x
    197 and the second
    198 .Sq x
    199 stands for itself, so that the regular expression is
    200 .Dq abcxdef .
    201 .Pp
    202 .It
    203 The escape sequence \en matches a newline character embedded in the
    204 pattern space.
    205 You can't, however, use a literal newline character in an address or
    206 in the substitute command.
    207 .El
    208 .Pp
    209 One special feature of
    210 .Nm
    211 regular expressions is that they can default to the last regular
    212 expression used.
    213 If a regular expression is empty, i.e., just the delimiter characters
    214 are specified, the last regular expression encountered is used instead.
    215 The last regular expression is defined as the last regular expression
    216 used as part of an address or substitute command, and at run-time, not
    217 compile-time.
    218 For example, the command
    219 .Dq /abc/s//XXX/
    220 will substitute
    221 .Dq XXX
    222 for the pattern
    223 .Dq abc .
    224 .Sh SED FUNCTIONS
    225 In the following list of commands, the maximum number of permissible
    226 addresses for each command is indicated by [0addr], [1addr], or [2addr],
    227 representing zero, one, or two addresses.
    228 .Pp
    229 The argument
    230 .Ar text
    231 consists of one or more lines.
    232 To embed a newline in the text, precede it with a backslash.
    233 Other backslashes in text are deleted and the following character
    234 taken literally.
    235 .Pp
    236 The
    237 .Ic r
    238 and
    239 .Ic w
    240 functions,
    241 as well as the
    242 .Cm w
    243 flag to the
    244 .Ic s
    245 function,
    246 take an optional
    247 .Ar file
    248 parameter,
    249 which should be separated from the function or flag by whitespace.
    250 Files are created
    251 (or their contents truncated)
    252 before any input processing begins.
    253 .Pp
    254 The
    255 .Ic b ,
    256 .Ic r ,
    257 .Ic s ,
    258 .Ic t ,
    259 .Ic w ,
    260 .Ic y ,
    261 and
    262 .Ic \&:
    263 functions all accept additional arguments.
    264 The synopses below indicate which arguments have to be separated from
    265 the function letters by whitespace characters.
    266 .Pp
    267 Functions can be combined to form a
    268 .Em function list ,
    269 a list of
    270 .Nm
    271 functions each followed by a newline, as follows:
    272 .Bd -literal -offset indent
    273 { function
    274   function
    275   ...
    276   function
    277 }
    278 .Ed
    279 .Pp
    280 The braces can be preceded and followed by whitespace.
    281 The functions can be preceded by whitespace as well.
    282 .Pp
    283 Functions and function lists may be preceded by an exclamation mark,
    284 in which case they are applied only to lines that are
    285 .Em not
    286 selected by the addresses.
    287 .Bl -tag -width Ds
    288 .It [2addr] Ar function-list
    289 Execute
    290 .Ar function-list
    291 only when the pattern space is selected.
    292 .It Xo [1 addr] Ic a Ns \e
    293 .br
    294 .Ar text
    295 .Xc
    296 .Pp
    297 Write
    298 .Ar text
    299 to standard output immediately before each attempt to read a line of input,
    300 whether by executing the
    301 .Ic N
    302 function or by beginning a new cycle.
    303 .It [2addr] Ns Ic b Bq Ar label
    304 Branch to the
    305 .Ic \&:
    306 function with the specified
    307 .Ar label .
    308 If the label is not specified, branch to the end of the script.
    309 .It Xo [2addr] Ic c Ns \e
    310 .br
    311 .Ar text
    312 .Xc
    313 .Pp
    314 Delete the pattern space.
    315 With 0 or 1 address or at the end of a 2-address range,
    316 .Ar text
    317 is written to the standard output.
    318 .It [2addr] Ns Ic d
    319 Delete the pattern space and start the next cycle.
    320 .It [2addr] Ns Ic D
    321 Delete the initial segment of the pattern space through the first
    322 newline character and start the next cycle.
    323 .It [2addr] Ns Ic g
    324 Replace the contents of the pattern space with the contents of the
    325 hold space.
    326 .It [2addr] Ns Ic G
    327 Append a newline character followed by the contents of the hold space
    328 to the pattern space.
    329 .It [2addr] Ns Ic h
    330 Replace the contents of the hold space with the contents of the
    331 pattern space.
    332 .It [2addr] Ns Ic H
    333 Append a newline character followed by the contents of the pattern space
    334 to the hold space.
    335 .It Xo [1addr] Ic i Ns \e
    336 .br
    337 .Ar text
    338 .Xc
    339 .Pp
    340 Write
    341 .Ar text
    342 to the standard output.
    343 .It [2addr] Ns Ic l
    344 (The letter ell.)
    345 Write the pattern space to the standard output in a visually unambiguous
    346 form.
    347 This form is as follows:
    348 .Pp
    349 .Bl -tag -width "carriage-returnXX" -offset indent -compact
    350 .It backslash
    351 \e\e
    352 .It alert
    353 \ea
    354 .It backspace
    355 \eb
    356 .It form-feed
    357 \ef
    358 .It carriage-return
    359 \er
    360 .It tab
    361 \et
    362 .It vertical tab
    363 \ev
    364 .El
    365 .Pp
    366 Non-printable characters are written as three-digit octal numbers (with a
    367 preceding backslash) for each byte in the character (most significant byte
    368 first).
    369 Long lines are folded, with the point of folding indicated by displaying
    370 a backslash followed by a newline.
    371 The end of each line is marked with a
    372 .Ql $ .
    373 .It [2addr] Ns Ic n
    374 Write the pattern space to the standard output if the default output has
    375 not been suppressed, and replace the pattern space with the next line of
    376 input.
    377 .It [2addr] Ns Ic N
    378 Append the next line of input to the pattern space, using an embedded
    379 newline character to separate the appended material from the original
    380 contents.
    381 Note that the current line number changes.
    382 .It [2addr] Ns Ic p
    383 Write the pattern space to standard output.
    384 .It [2addr] Ns Ic P
    385 Write the pattern space, up to the first newline character,
    386 to the standard output.
    387 .It [1addr] Ns Ic q
    388 Branch to the end of the script and quit without starting a new cycle.
    389 .It [1addr] Ns Ic r Ar file
    390 Copy the contents of
    391 .Ar file
    392 to the standard output immediately before the next attempt to read a
    393 line of input.
    394 If
    395 .Ar file
    396 cannot be read for any reason, it is silently ignored and no error
    397 condition is set.
    398 .It [2addr] Ns Ic s Ns / Ns Ar RE Ns / Ns Ar replacement Ns / Ns Ar flags
    399 Substitute the
    400 .Ar replacement
    401 string for the first instance of the regular expression
    402 .Ar RE
    403 in the pattern space.
    404 Any character other than backslash or newline can be used instead of
    405 a slash to delimit the regular expression and the replacement.
    406 Within the regular expression and the replacement,
    407 the regular expression delimiter itself can be used as
    408 a literal character if it is preceded by a backslash.
    409 .Pp
    410 An ampersand
    411 .Pq Ql &
    412 appearing in the replacement is replaced by the string matching the
    413 regular expression.
    414 The special meaning of
    415 .Ql &
    416 in this context can be suppressed by preceding it by a backslash.
    417 The string
    418 .Ql \e# ,
    419 where
    420 .Ql #
    421 is a digit, is replaced by the text matched
    422 by the corresponding backreference expression (see
    423 .Xr re_format 7 ) .
    424 .Pp
    425 A line can be split by substituting a newline character into it.
    426 To specify a newline character in the replacement string, precede it with
    427 a backslash.
    428 .Pp
    429 The value of
    430 .Ar flags
    431 in the substitute function is zero or more of the following:
    432 .Bl -tag -width "XXXXXX" -offset indent
    433 .It Cm 0 No ... Cm 9
    434 Make the substitution only for the N'th occurrence of the regular
    435 expression in the pattern space.
    436 .It Cm g
    437 Make the substitution for all non-overlapping matches of the
    438 regular expression, not just the first one.
    439 .It Cm p
    440 Write the pattern space to standard output if a replacement was made.
    441 If the replacement string is identical to that which it replaces, it
    442 is still considered to have been a replacement.
    443 .It Cm w Ar file
    444 Append the pattern space to
    445 .Ar file
    446 if a replacement was made.
    447 If the replacement string is identical to that which it replaces, it
    448 is still considered to have been a replacement.
    449 .El
    450 .It [2addr] Ns Ic t Bq Ar label
    451 Branch to the
    452 .Ic \&:
    453 function bearing the
    454 .Ar label
    455 if any substitutions have been made since the
    456 most recent reading of an input line or execution of a
    457 .Ic t
    458 function.
    459 If no label is specified, branch to the end of the script.
    460 .It [2addr] Ns Ic w Ar file
    461 Append the pattern space to the
    462 .Ar file .
    463 .It [2addr] Ns Ic x
    464 Swap the contents of the pattern and hold spaces.
    465 .It [2addr] Ns Ic y Ns / Ns Ar string1 Ns / Ns Ar string2 Ns /
    466 Replace all occurrences of characters in
    467 .Ar string1
    468 in the pattern space with the corresponding characters from
    469 .Ar string2 .
    470 Any character other than a backslash or newline can be used instead of
    471 a slash to delimit the strings.
    472 Within
    473 .Ar string1
    474 and
    475 .Ar string2 ,
    476 a backslash followed by any character other than a newline is that literal
    477 character, and a backslash followed by an
    478 .Sq n
    479 is replaced by a newline character.
    480 .It [0addr] Ns Ic \&: Ns Ar label
    481 This function does nothing; it bears a
    482 .Ar label
    483 to which the
    484 .Ic b
    485 and
    486 .Ic t
    487 commands may branch.
    488 .It [1addr] Ns Ic =
    489 Write the line number to the standard output followed by a newline character.
    490 .It [0addr]
    491 Empty lines are ignored.
    492 .It [0addr] Ns Ic #
    493 The
    494 .Ql #
    495 and the remainder of the line are ignored (treated as a comment), with
    496 the single exception that if the first two characters in the file are
    497 .Ql #n ,
    498 the default output is suppressed.
    499 This is the same as specifying the
    500 .Fl n
    501 option on the command line.
    502 .El
    503 .Sh EXIT STATUS
    504 .Ex -std sed
    505 .Sh EXAMPLES
    506 The following simulates the
    507 .Xr cat 1
    508 .Fl s
    509 command,
    510 squeezing excess empty lines from standard input:
    511 .Bd -literal -offset indent
    512 $ sed -n '
    513 # Write non-empty lines.
    514 /./ {
    515     p
    516     d
    517     }
    518 # Write a single empty line, then look for more empty lines.
    519 /^$/    p
    520 # Get the next line, discard the held <newline> (empty line),
    521 # and look for more empty lines.
    522 :Empty
    523 /^$/    {
    524     N
    525     s/.//
    526     b Empty
    527     }
    528 # Write the non-empty line before going back to search
    529 # for the first in a set of empty lines.
    530     p
    531 \&'
    532 .Ed
    533 .Sh SEE ALSO
    534 .Xr awk 1 ,
    535 .Xr ed 1 ,
    536 .Xr grep 1 ,
    537 .Xr re_format 7
    538 .Sh STANDARDS
    539 The
    540 .Nm
    541 utility is compliant with the
    542 .St -p1003.1-2008
    543 specification.
    544 .Pp
    545 The flags
    546 .Op Fl aEru
    547 are extensions to that specification.
    548 .Pp
    549 The use of newlines to separate multiple commands on the command line
    550 is non-portable;
    551 the use of newlines to separate multiple commands within a command file
    552 .Pq Fl f Ar command_file
    553 is portable.
    554 .Sh HISTORY
    555 A
    556 .Nm
    557 command appeared in
    558 .At v7 .
    559 .Sh CAVEATS
    560 The use of semicolons to separate multiple commands
    561 is not permitted for the following commands:
    562 .Ic a , b , c ,
    563 .Ic i , r , t ,
    564 .Ic w , \&: ,
    565 and
    566 .Ic # .