[BACK]Return to sed.1 CVS log [TXT][DIR] Up to [local] / src / usr.bin / sed

Annotation of src/usr.bin/sed/sed.1, Revision 1.35

1.35    ! jmc         1: .\"    $OpenBSD: sed.1,v 1.34 2009/08/07 06:37:50 jmc Exp $
1.12      aaron       2: .\"
1.1       deraadt     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.
1.17      millert    17: .\" 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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: .\"
1.34      jmc        35: .Dd $Mdocdate: August 7 2009 $
1.1       deraadt    36: .Dt SED 1
                     37: .Os
                     38: .Sh NAME
                     39: .Nm sed
                     40: .Nd stream editor
                     41: .Sh SYNOPSIS
                     42: .Nm sed
1.34      jmc        43: .Op Fl aEnru
1.1       deraadt    44: .Ar command
1.29      sobrado    45: .Op Ar
1.1       deraadt    46: .Nm sed
1.33      djm        47: .Op Fl aEnru
1.1       deraadt    48: .Op Fl e Ar command
                     49: .Op Fl f Ar command_file
1.29      sobrado    50: .Op Ar
1.1       deraadt    51: .Sh DESCRIPTION
                     52: The
1.8       aaron      53: .Nm
1.1       deraadt    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 .
1.25      jmc        60: Multiple commands may be specified
                     61: separated by newlines or semicolons,
                     62: or by using the
1.1       deraadt    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
1.10      aaron      70: The options are as follows:
1.16      aaron      71: .Bl -tag -width Ds
1.1       deraadt    72: .It Fl a
                     73: The files listed as parameters for the
1.8       aaron      74: .Ql w
1.1       deraadt    75: functions are created (or truncated) before any processing begins,
                     76: by default.
                     77: The
                     78: .Fl a
                     79: option causes
1.8       aaron      80: .Nm
1.1       deraadt    81: to delay opening each file until a command containing the related
1.8       aaron      82: .Ql w
1.1       deraadt    83: function is applied to a line of input.
1.34      jmc        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.
1.1       deraadt    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.
1.33      djm        97: .It Fl r
1.34      jmc        98: An alias for
                     99: .Fl E ,
                    100: for compatibility with GNU sed.
1.1       deraadt   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.
1.26      ray       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.
1.1       deraadt   115: .El
                    116: .Pp
                    117: The form of a
1.8       aaron     118: .Nm
1.1       deraadt   119: command is as follows:
1.21      jmc       120: .Pp
1.1       deraadt   121: .Dl [address[,address]]function[arguments]
1.21      jmc       122: .Pp
1.1       deraadt   123: Whitespace may be inserted before the first address and the function
                    124: portions of the command.
                    125: .Pp
                    126: Normally,
1.8       aaron     127: .Nm
1.1       deraadt   128: cyclically copies a line of input, not including its terminating newline
                    129: character, into a
1.21      jmc       130: .Em pattern space ,
1.1       deraadt   131: (unless there is something left after a
1.8       aaron     132: .Sq D
1.1       deraadt   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
1.21      jmc       139: .Em hold space
1.1       deraadt   140: to save all or part of the pattern space for subsequent retrieval.
1.21      jmc       141: .Sh SED ADDRESSES
1.1       deraadt   142: An address is not required, but if specified must be a number (that counts
                    143: input lines
1.21      jmc       144: cumulatively across input files), a dollar character
1.8       aaron     145: .Pq Ql $
1.21      jmc       146: that addresses the last line of input, or a context address
1.1       deraadt   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,
1.8       aaron     161: .Nm
1.1       deraadt   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
1.18      jmc       166: .Pq Ql \&!
1.1       deraadt   167: function.
1.21      jmc       168: .Sh SED REGULAR EXPRESSIONS
1.34      jmc       169: By default,
1.8       aaron     170: .Nm
1.23      jmc       171: regular expressions are basic regular expressions
                    172: .Pq BREs .
1.34      jmc       173: Extended regular expressions are supported using the
                    174: .Fl E
                    175: and
                    176: .Fl r
                    177: options.
1.23      jmc       178: See
1.19      jmc       179: .Xr re_format 7
1.23      jmc       180: for more information on regular expressions.
1.1       deraadt   181: In addition,
1.8       aaron     182: .Nm
1.23      jmc       183: has the following two additions to BREs:
1.21      jmc       184: .Pp
1.1       deraadt   185: .Bl -enum -compact
                    186: .It
                    187: In a context address, any character other than a backslash
1.8       aaron     188: .Pq Ql \e
1.1       deraadt   189: or newline character may be used to delimit the regular expression.
1.30      jmc       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
1.1       deraadt   193: causes the character to be treated literally.
                    194: For example, in the context address \exabc\exdefx, the RE delimiter
                    195: is an
1.8       aaron     196: .Sq x
1.1       deraadt   197: and the second
1.8       aaron     198: .Sq x
1.1       deraadt   199: stands for itself, so that the regular expression is
                    200: .Dq abcxdef .
1.21      jmc       201: .Pp
1.1       deraadt   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
1.8       aaron     210: .Nm
1.1       deraadt   211: regular expressions is that they can default to the last regular
                    212: expression used.
1.13      aaron     213: If a regular expression is empty, i.e., just the delimiter characters
1.1       deraadt   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 .
1.21      jmc       224: .Sh SED FUNCTIONS
1.1       deraadt   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: .Em 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
1.8       aaron     237: .Sq r
1.1       deraadt   238: and
1.8       aaron     239: .Sq w
1.1       deraadt   240: functions take an optional file parameter, which should be separated
1.9       aaron     241: from the function letter by whitespace.
1.1       deraadt   242: Each file given as an argument to
1.8       aaron     243: .Nm
1.1       deraadt   244: is created (or its contents truncated) before any input processing begins.
                    245: .Pp
                    246: The
1.8       aaron     247: .Sq b ,
                    248: .Sq r ,
                    249: .Sq s ,
                    250: .Sq t ,
                    251: .Sq w ,
                    252: .Sq y ,
1.18      jmc       253: .Ql \&! ,
1.1       deraadt   254: and
1.8       aaron     255: .Ql \&:
1.1       deraadt   256: functions all accept additional arguments.
                    257: The following synopses indicate which arguments have to be separated from
1.9       aaron     258: the function letters by whitespace characters.
1.1       deraadt   259: .Pp
                    260: Two of the functions take a function-list.
                    261: This is a list of
1.8       aaron     262: .Nm
1.1       deraadt   263: functions separated by newlines, as follows:
                    264: .Bd -literal -offset indent
                    265: { function
                    266:   function
                    267:   ...
                    268:   function
                    269: }
                    270: .Ed
                    271: .Pp
                    272: The
1.8       aaron     273: .Ql {
1.9       aaron     274: can be preceded or followed by whitespace.
                    275: The function can be preceded by whitespace as well.
1.1       deraadt   276: The terminating
1.8       aaron     277: .Ql }
1.9       aaron     278: must be preceded by a newline or optional whitespace.
1.21      jmc       279: .Pp
                    280: .Bl -tag -width "XXXXXXXX" -compact
1.15      aaron     281: .It [2addr] Em function-list
                    282: Execute
                    283: .Em function-list
                    284: only when the pattern space is selected.
1.21      jmc       285: .Pp
1.20      jmc       286: .It [1addr] Ns Em a Ns \e
1.15      aaron     287: .It Em text
1.21      jmc       288: .Pp
1.1       deraadt   289: Write
                    290: .Em text
                    291: to standard output immediately before each attempt to read a line of input,
                    292: whether by executing the
1.8       aaron     293: .Sq N
1.1       deraadt   294: function or by beginning a new cycle.
1.21      jmc       295: .Pp
1.20      jmc       296: .It [2addr] Ns Em b Ns [label]
1.1       deraadt   297: Branch to the
1.8       aaron     298: .Sq \&:
1.1       deraadt   299: function with the specified label.
                    300: If the label is not specified, branch to the end of the script.
1.21      jmc       301: .Pp
1.20      jmc       302: .It [2addr] Ns Em c Ns \e
1.15      aaron     303: .It Em text
1.21      jmc       304: .Pp
1.1       deraadt   305: Delete the pattern space.
                    306: With 0 or 1 address or at the end of a 2-address range,
                    307: .Em text
                    308: is written to the standard output.
1.21      jmc       309: .Pp
1.15      aaron     310: .It [2addr] Ns Em d
1.1       deraadt   311: Delete the pattern space and start the next cycle.
1.21      jmc       312: .Pp
1.15      aaron     313: .It [2addr] Ns Em D
1.1       deraadt   314: Delete the initial segment of the pattern space through the first
                    315: newline character and start the next cycle.
1.21      jmc       316: .Pp
1.15      aaron     317: .It [2addr] Ns Em g
1.1       deraadt   318: Replace the contents of the pattern space with the contents of the
                    319: hold space.
1.21      jmc       320: .Pp
1.15      aaron     321: .It [2addr] Ns Em G
1.1       deraadt   322: Append a newline character followed by the contents of the hold space
                    323: to the pattern space.
1.21      jmc       324: .Pp
1.15      aaron     325: .It [2addr] Ns Em h
1.1       deraadt   326: Replace the contents of the hold space with the contents of the
                    327: pattern space.
1.21      jmc       328: .Pp
1.15      aaron     329: .It [2addr] Ns Em H
1.1       deraadt   330: Append a newline character followed by the contents of the pattern space
                    331: to the hold space.
1.21      jmc       332: .Pp
1.20      jmc       333: .It [1addr] Ns Em i Ns \e
1.15      aaron     334: .It Em text
1.21      jmc       335: .Pp
1.1       deraadt   336: Write
                    337: .Em text
                    338: to the standard output.
1.21      jmc       339: .Pp
1.15      aaron     340: .It [2addr] Ns Em l
1.1       deraadt   341: (The letter ell.)
                    342: Write the pattern space to the standard output in a visually unambiguous
                    343: form.
                    344: This form is as follows:
1.21      jmc       345: .Pp
1.1       deraadt   346: .Bl -tag -width "carriage-returnXX" -offset indent -compact
                    347: .It backslash
1.3       deraadt   348: \e\e
1.1       deraadt   349: .It alert
                    350: \ea
1.31      millert   351: .It backspace
                    352: \eb
1.1       deraadt   353: .It form-feed
                    354: \ef
                    355: .It carriage-return
                    356: \er
                    357: .It tab
                    358: \et
                    359: .It vertical tab
                    360: \ev
                    361: .El
                    362: .Pp
1.15      aaron     363: Non-printable characters are written as three-digit octal numbers (with a
1.1       deraadt   364: preceding backslash) for each byte in the character (most significant byte
                    365: first).
                    366: Long lines are folded, with the point of folding indicated by displaying
                    367: a backslash followed by a newline.
                    368: The end of each line is marked with a
1.8       aaron     369: .Ql $ .
1.21      jmc       370: .Pp
1.15      aaron     371: .It [2addr] Ns Em n
1.1       deraadt   372: Write the pattern space to the standard output if the default output has
                    373: not been suppressed, and replace the pattern space with the next line of
                    374: input.
1.21      jmc       375: .Pp
1.15      aaron     376: .It [2addr] Ns Em N
1.1       deraadt   377: Append the next line of input to the pattern space, using an embedded
                    378: newline character to separate the appended material from the original
                    379: contents.
                    380: Note that the current line number changes.
1.21      jmc       381: .Pp
1.15      aaron     382: .It [2addr] Ns Em p
1.1       deraadt   383: Write the pattern space to standard output.
1.21      jmc       384: .Pp
1.15      aaron     385: .It [2addr] Ns Em P
1.1       deraadt   386: Write the pattern space, up to the first newline character to the
                    387: standard output.
1.21      jmc       388: .Pp
1.15      aaron     389: .It [1addr] Ns Em q
1.1       deraadt   390: Branch to the end of the script and quit without starting a new cycle.
1.21      jmc       391: .Pp
1.15      aaron     392: .It [1addr] Ns Em r file
1.1       deraadt   393: Copy the contents of
                    394: .Em file
                    395: to the standard output immediately before the next attempt to read a
                    396: line of input.
                    397: If
                    398: .Em file
                    399: cannot be read for any reason, it is silently ignored and no error
                    400: condition is set.
1.21      jmc       401: .Pp
1.20      jmc       402: .It [2addr] Ns Em s Ns /re/replacement/flags
1.1       deraadt   403: Substitute the replacement string for the first instance of the regular
                    404: expression in the pattern space.
                    405: Any character other than backslash or newline can be used instead of
                    406: a slash to delimit the RE and the replacement.
                    407: Within the RE and the replacement, the RE delimiter itself can be used as
                    408: a literal character if it is preceded by a backslash.
                    409: .Pp
                    410: An ampersand
1.8       aaron     411: .Pq Ql &
1.1       deraadt   412: appearing in the replacement is replaced by the string matching the RE.
                    413: The special meaning of
1.8       aaron     414: .Ql &
1.1       deraadt   415: in this context can be suppressed by preceding it by a backslash.
                    416: The string
1.8       aaron     417: .Ql \e# ,
1.1       deraadt   418: where
1.8       aaron     419: .Ql #
1.1       deraadt   420: is a digit, is replaced by the text matched
                    421: by the corresponding backreference expression (see
1.14      aaron     422: .Xr re_format 7 ) .
1.1       deraadt   423: .Pp
                    424: A line can be split by substituting a newline character into it.
                    425: To specify a newline character in the replacement string, precede it with
                    426: a backslash.
                    427: .Pp
                    428: The value of
                    429: .Em flags
                    430: in the substitute function is zero or more of the following:
                    431: .Bl -tag -width "XXXXXX" -offset indent
1.21      jmc       432: .It 0 ... 9
1.1       deraadt   433: Make the substitution only for the N'th occurrence of the regular
                    434: expression in the pattern space.
                    435: .It g
                    436: Make the substitution for all non-overlapping matches of the
                    437: regular expression, not just the first one.
                    438: .It p
                    439: Write the pattern space to standard output if a replacement was made.
                    440: If the replacement string is identical to that which it replaces, it
                    441: is still considered to have been a replacement.
                    442: .It w Em file
                    443: Append the pattern space to
                    444: .Em file
                    445: if a replacement was made.
                    446: If the replacement string is identical to that which it replaces, it
                    447: is still considered to have been a replacement.
                    448: .El
1.21      jmc       449: .Pp
1.20      jmc       450: .It [2addr] Ns Em t Ns [label]
1.1       deraadt   451: Branch to the
1.8       aaron     452: .Ql \&:
1.1       deraadt   453: function bearing the label if any substitutions have been made since the
                    454: most recent reading of an input line or execution of a
1.8       aaron     455: .Sq t
1.1       deraadt   456: function.
                    457: If no label is specified, branch to the end of the script.
1.21      jmc       458: .Pp
1.15      aaron     459: .It [2addr] Ns Em w file
1.1       deraadt   460: Append the pattern space to the
                    461: .Em file .
1.21      jmc       462: .Pp
1.15      aaron     463: .It [2addr] Ns Em x
1.1       deraadt   464: Swap the contents of the pattern and hold spaces.
1.21      jmc       465: .Pp
1.20      jmc       466: .It [2addr] Ns Em y Ns /string1/string2/
1.1       deraadt   467: Replace all occurrences of characters in
                    468: .Em string1
                    469: in the pattern space with the corresponding characters from
                    470: .Em string2 .
                    471: Any character other than a backslash or newline can be used instead of
                    472: a slash to delimit the strings.
                    473: Within
                    474: .Em string1
                    475: and
                    476: .Em string2 ,
                    477: a backslash followed by any character other than a newline is that literal
1.8       aaron     478: character, and a backslash followed by an
                    479: .Sq n
                    480: is replaced by a newline character.
1.21      jmc       481: .Pp
1.15      aaron     482: .Sm off
1.21      jmc       483: .It Xo [2addr] Em !function No ,\ \&[2addr]
1.15      aaron     484: .Em !function-list
                    485: .Xc
                    486: .Sm on
1.1       deraadt   487: Apply the function or function-list only to the lines that are
                    488: .Em not
                    489: selected by the address(es).
1.21      jmc       490: .Pp
                    491: .It [0addr] Ns Em \&: Ns label
1.1       deraadt   492: This function does nothing; it bears a label to which the
1.8       aaron     493: .Sq b
1.1       deraadt   494: and
1.8       aaron     495: .Sq t
1.1       deraadt   496: commands may branch.
1.21      jmc       497: .Pp
1.15      aaron     498: .It [1addr] Ns Em =
                    499: Write the line number to the standard output followed by a newline character.
1.21      jmc       500: .Pp
1.1       deraadt   501: .It [0addr]
                    502: Empty lines are ignored.
1.21      jmc       503: .Pp
1.15      aaron     504: .It [0addr] Ns Em #
1.1       deraadt   505: The
1.8       aaron     506: .Ql #
1.1       deraadt   507: and the remainder of the line are ignored (treated as a comment), with
                    508: the single exception that if the first two characters in the file are
1.8       aaron     509: .Ql #n ,
1.1       deraadt   510: the default output is suppressed.
                    511: This is the same as specifying the
                    512: .Fl n
                    513: option on the command line.
                    514: .El
                    515: .Pp
1.24      jmc       516: .Ex -std sed
1.1       deraadt   517: .Sh SEE ALSO
                    518: .Xr awk 1 ,
                    519: .Xr ed 1 ,
                    520: .Xr grep 1 ,
                    521: .Xr regex 3 ,
1.26      ray       522: .Xr setbuf 3 ,
1.1       deraadt   523: .Xr re_format 7
                    524: .Sh STANDARDS
                    525: The
1.8       aaron     526: .Nm
1.25      jmc       527: utility is compliant with the
1.32      jmc       528: .St -p1003.1-2008
1.1       deraadt   529: specification.
1.25      jmc       530: .Pp
1.26      ray       531: The flags
1.33      djm       532: .Op Fl aEru
1.27      jmc       533: are extensions to that specification.
1.25      jmc       534: .Pp
                    535: The use of newlines to separate multiple commands on the command line
                    536: is non-portable;
                    537: the use of newlines to separate multiple commands within a command file
                    538: .Pq Fl f Ar command_file
                    539: is portable.
1.11      aaron     540: .Sh HISTORY
                    541: A
                    542: .Nm
                    543: command appeared in
                    544: .At v7 .
1.25      jmc       545: .Sh CAVEATS
                    546: The use of semicolons to separate multiple commands
                    547: is not permitted for the following commands:
                    548: .Cm a , b , c ,
                    549: .Cm i , r , t ,
                    550: .Cm w , \&: ,
                    551: and
                    552: .Cm # .