1 SED(1)                           User Commands                          SED(1)
   2 
   3 
   4 
   5 NAME
   6        sed - stream editor
   7 
   8 SYNOPSIS
   9        sed [-Ealnr] command [file ...]
  10        sed [-Ealnr] [-e command] [-f command_file] [-I[extension] |
  11        -i[extension]] [file ...]
  12 
  13 DESCRIPTION
  14        The sed utility reads the specified files, or the standard input if no
  15        files are specified, modifying the input as specified by a list of
  16        commands.  The input is then written to the standard output.
  17 
  18        A single command may be specified as the first argument to . Multiple
  19        commands may be specified by using the -e or -f options.  All commands
  20        are applied to the input in the order they are specified regardless of
  21        their origin.
  22 
  23        The following options are available:
  24 
  25        -E     Interpret regular expressions as extended (modern) regular
  26               expressions rather than basic regular expressions (BRE's).  The
  27               regex(5) manual page fully describes both formats.
  28 
  29        -a     The files listed as parameters for the ``w'' functions are
  30               created (or truncated) before any processing begins, by default.
  31               The -a option causes sed to delay opening each file until a
  32               command containing the related ``w'' function is applied to a
  33               line of input.
  34 
  35        -e command
  36               Append the editing commands specified by the command argument to
  37               the list of commands.
  38 
  39        -f command_file
  40               Append the editing commands found in the file command_file to
  41               the list of commands.  The editing commands should each be
  42               listed on a separate line.
  43 
  44        -I[extension]
  45               Edit files in-place, saving backups if extension was specified.
  46               It is not recommended to omit saving backups when in-place
  47               editing files, as you risk corruption or partial content in
  48               situations where disk space is exhausted, etc.
  49 
  50               Note that in-place editing with -I still takes place in a single
  51               continuous line address space covering all files, although each
  52               file preserves its individuality instead of forming one output
  53               stream.  The line counter is never reset between files, address
  54               ranges can span file boundaries, and the ``$'' address matches
  55               only the last line of the last file.  (See Sed Addresses . )
  56               That can lead to unexpected results in many cases of in-place
  57               editing, where using -i is desired.
  58 
  59        -i[extension]
  60               Edit files in-place similarly to -I, but treat each file
  61               independently from other files.  In particular, line numbers in
  62               each file start at 1, the ``$'' address matches the last line of
  63               the current file, and address ranges are limited to the current
  64               file.  (See Sed Addresses . ) The net result is as though each
  65               file were edited by a separate sed instance.
  66 
  67        -l     Make output line buffered.
  68 
  69        -n     By default, each line of input is echoed to the standard output
  70               after all of the commands have been applied to it.  The -n
  71               option suppresses this behavior.
  72 
  73        -r     Same as -E for compatibility with GNU sed.
  74 
  75               The form of a sed command is as follows:
  76 
  77               [address[,address]]function[arguments]
  78 
  79               Whitespace may be inserted before the first address and the
  80               function portions of the command.
  81 
  82               Normally, sed cyclically copies a line of input, not including
  83               its terminating newline character, into a pattern space, (unless
  84               there is something left after a ``D'' function), applies all of
  85               the commands with addresses that select that pattern space,
  86               copies the pattern space to the standard output, appending a
  87               newline, and deletes the pattern space.
  88 
  89               Some of the functions use a hold space to save all or part of
  90               the pattern space for subsequent retrieval.
  91 
  92 Sed Addresses
  93        An address is not required, but if specified must have one of the
  94        following formats:
  95 
  96        o      a number that counts input lines cumulatively across input files
  97               (or in each file independently if a -i option is in effect);
  98 
  99        o      a dollar (``$'') character that addresses the last line of input
 100               (or the last line of the current file if a -i option was
 101               specified);
 102 
 103        o      a context address that consists of a regular expression preceded
 104               and followed by a delimiter. The closing delimiter can also
 105               optionally be followed by the ``I'' character, to indicate that
 106               the regular expression is to be matched in a case-insensitive
 107               way.
 108 
 109               A command line with no addresses selects every pattern space.
 110 
 111               A command line with one address selects all of the pattern
 112               spaces that match the address.
 113 
 114               A command line with two addresses selects an inclusive range.
 115               This range starts with the first pattern space that matches the
 116               first address.  The end of the range is the next following
 117               pattern space that matches the second address.  If the second
 118               address is a number less than or equal to the line number first
 119               selected, only that line is selected.  The number in the second
 120               address may be prefixed with a (``+'') to specify the number of
 121               lines to match after the first pattern.  In the case when the
 122               second address is a context address, sed does not re-match the
 123               second address against the pattern space that matched the first
 124               address.  Starting at the first line following the selected
 125               range, sed starts looking again for the first address.
 126 
 127               Editing commands can be applied to non-selected pattern spaces
 128               by use of the exclamation character (``!'') function.
 129 
 130 Sed Regular Expressions
 131        The regular expressions used in , by default, are basic regular
 132        expressions (BREs, see regex(5) for more information), but extended
 133        (modern) regular expressions can be used instead if the -E flag is
 134        given.  In addition, sed has the following two additions to regular
 135        expressions:
 136 
 137 
 138        1.     In a context address, any character other than a backslash
 139               (``\'') or newline character may be used to delimit the regular
 140               expression.  The opening delimiter needs to be preceded by a
 141               backslash unless it is a slash.  For example, the context
 142               address \xabcx is equivalent to /abc/ .  Also, putting a
 143               backslash character before the delimiting character within the
 144               regular expression causes the character to be treated literally.
 145               For example, in the context address \xabc\xdefx , the RE
 146               delimiter is an ``x'' and the second ``x'' stands for itself, so
 147               that the regular expression is ``abcxdef''.
 148 
 149 
 150        2.     The escape sequence \n matches a newline character embedded in
 151               the pattern space.  You cannot, however, use a literal newline
 152               character in an address or in the substitute command.
 153 
 154               One special feature of sed regular expressions is that they can
 155               default to the last regular expression used.  If a regular
 156               expression is empty, i.e., just the delimiter characters are
 157               specified, the last regular expression encountered is used
 158               instead.  The last regular expression is defined as the last
 159               regular expression used as part of an address or substitute
 160               command, and at run-time, not compile-time.  For example, the
 161               command ``/abc/s//XXX/'' will substitute ``XXX'' for the pattern
 162               ``abc''.
 163 
 164 Sed Functions
 165        In the following list of commands, the maximum number of permissible
 166        addresses for each command is indicated by [0addr], [1addr], or
 167        [2addr], representing zero, one, or two addresses.
 168 
 169        The argument text consists of one or more lines.  To embed a newline in
 170        the text, precede it with a backslash.  Other backslashes in text are
 171        deleted and the following character taken literally.
 172 
 173        The ``r'' and ``w'' functions take an optional file parameter, which
 174        should be separated from the function letter by white space.  Each file
 175        given as an argument to sed is created (or its contents truncated)
 176        before any input processing begins.
 177 
 178        The ``b'', ``r'', ``s'', ``t'', ``w'', ``y'', ``!'', and ``:''
 179        functions all accept additional arguments.  The following synopses
 180        indicate which arguments have to be separated from the function letters
 181        by white space characters.
 182 
 183        Two of the functions take a function-list.  This is a list of sed
 184        functions separated by newlines, as follows:
 185 
 186        { function
 187          function
 188          ...
 189          function
 190        }
 191 
 192        The ``{'' can be preceded by white space and can be followed by white
 193        space.  The function can be preceded by white space.  The terminating
 194        ``}'' must be preceded by a newline or optional white space.
 195 
 196 
 197        [2addr] function-list
 198               Execute function-list only when the pattern space is selected.
 199 
 200 
 201        [1addr]a\
 202 
 203        text   Write text to standard output immediately before each attempt to
 204               read a line of input, whether by executing the ``N'' function or
 205               by beginning a new cycle.
 206 
 207 
 208        [2addr]b[label]
 209               Branch to the ``:'' function with the specified label.  If the
 210               label is not specified, branch to the end of the script.
 211 
 212 
 213        [2addr]c\
 214 
 215        text   Delete the pattern space.  With 0 or 1 address or at the end of
 216               a 2-address range, text is written to the standard output.
 217 
 218 
 219        [2addr]d
 220               Delete the pattern space and start the next cycle.
 221 
 222 
 223        [2addr]D
 224               Delete the initial segment of the pattern space through the
 225               first newline character and start the next cycle.
 226 
 227 
 228        [2addr]g
 229               Replace the contents of the pattern space with the contents of
 230               the hold space.
 231 
 232 
 233        [2addr]G
 234               Append a newline character followed by the contents of the hold
 235               space to the pattern space.
 236 
 237 
 238        [2addr]h
 239               Replace the contents of the hold space with the contents of the
 240               pattern space.
 241 
 242 
 243        [2addr]H
 244               Append a newline character followed by the contents of the
 245               pattern space to the hold space.
 246 
 247 
 248        [1addr]i\
 249 
 250        text   Write text to the standard output.
 251 
 252 
 253        [2addr]l
 254               (The letter ell.)  Write the pattern space to the standard
 255               output in a visually unambiguous form.  This form is as follows:
 256 
 257 
 258        backslash
 259               \\
 260 
 261        alert  \a
 262 
 263        form-feed
 264               \f
 265 
 266        carriage-return
 267               \r
 268 
 269        tab    \t
 270 
 271        vertical tab
 272               \v
 273 
 274               Nonprintable characters are written as three-digit octal numbers
 275               (with a preceding backslash) for each byte in the character
 276               (most significant byte first).  Long lines are folded, with the
 277               point of folding indicated by displaying a backslash followed by
 278               a newline.  The end of each line is marked with a ``$''.
 279 
 280 
 281        [2addr]n
 282               Write the pattern space to the standard output if the default
 283               output has not been suppressed, and replace the pattern space
 284               with the next line of input.
 285 
 286 
 287        [2addr]N
 288               Append the next line of input to the pattern space, using an
 289               embedded newline character to separate the appended material
 290               from the original contents.  Note that the current line number
 291               changes.
 292 
 293 
 294        [2addr]p
 295               Write the pattern space to standard output.
 296 
 297 
 298        [2addr]P
 299               Write the pattern space, up to the first newline character to
 300               the standard output.
 301 
 302 
 303        [1addr]q
 304               Branch to the end of the script and quit without starting a new
 305               cycle.
 306 
 307 
 308        [1addr]r file
 309               Copy the contents of file to the standard output immediately
 310               before the next attempt to read a line of input.  If file cannot
 311               be read for any reason, it is silently ignored and no error
 312               condition is set.
 313 
 314 
 315        [2addr]s/regular expression/replacement/flags
 316               Substitute the replacement string for the first instance of the
 317               regular expression in the pattern space.  Any character other
 318               than backslash or newline can be used instead of a slash to
 319               delimit the RE and the replacement.  Within the RE and the
 320               replacement, the RE delimiter itself can be used as a literal
 321               character if it is preceded by a backslash.
 322 
 323               An ampersand (``&'') appearing in     the replacement is replaced by
 324               the string matching the RE.  The special meaning of ``&''     in
 325               this context can be suppressed by preceding it by a backslash.
 326               The string ``\#'', where ``#'' is a digit, is replaced by the
 327               text matched by the corresponding backreference expression (see
 328               regex(5)) .
 329 
 330               A line can be split by substituting a newline character into it.
 331               To specify a newline character in the replacement string,
 332               precede it with a backslash.
 333 
 334               The value of flags in the substitute function is zero or more of
 335               the following:
 336 
 337        N      Make the substitution only for the N'th occurrence of the
 338               regular expression in the pattern space.
 339 
 340        g      Make the substitution for all non-overlapping matches of the
 341               regular expression, not just the first one.
 342 
 343        p      Write the pattern space to standard output if a replacement was
 344               made.  If the replacement string is identical to that which it
 345               replaces, it is still considered to have been a replacement.
 346 
 347        w file Append the pattern space to file if a replacement was made.  If
 348               the replacement string is identical to that which it replaces,
 349               it is still considered to have been a replacement.
 350 
 351        I      Match the regular expression in a case-insensitive way.
 352 
 353 
 354        [2addr]t [label]
 355               Branch to the ``:'' function bearing the label if any
 356               substitutions have been made since the most recent reading of an
 357               input line or execution of a ``t'' function.  If no label is
 358               specified, branch to the end of the script.
 359 
 360 
 361        [2addr]w file
 362               Append the pattern space to the file.
 363 
 364 
 365        [2addr]x
 366               Swap the contents of the pattern and hold spaces.
 367 
 368 
 369        [2addr]y/string1/string2/
 370               Replace all occurrences of characters in string1 in the pattern
 371               space with the corresponding characters from string2.  Any
 372               character other than a backslash or newline can be used instead
 373               of a slash to delimit the strings.  Within string1 and string2,
 374               a backslash followed by any character other than a newline is
 375               that literal character, and a backslash followed by an ``n'' is
 376               replaced by a newline character.
 377 
 378 
 379        [2addr]!function
 380 
 381        [2addr]!function-list
 382               Apply the function or function-list only to the lines that are
 383               not selected by the address(es).
 384 
 385 
 386        [0addr]:label
 387               This function does nothing; it bears a label to which the ``b''
 388               and ``t'' commands may branch.
 389 
 390 
 391        [1addr]=
 392               Write the line number to the standard output followed by a
 393               newline character.
 394 
 395 
 396        [0addr]
 397               Empty lines are ignored.
 398 
 399 
 400        [0addr]#
 401               The ``#'' and the remainder of the line are ignored (treated as
 402               a comment), with the single exception that if the first two
 403               characters in the file are ``#n'', the default output is
 404               suppressed.  This is the same as specifying the -n option on the
 405               command line.
 406 
 407 ENVIRONMENT
 408        The COLUMNS,LANG,LC_ALL,LC_CTYPE and LC_COLLATE environment variables
 409        affect the execution of sed as described in environ(5).
 410 
 411 EXIT STATUS
 412        The sed utility exits 0 on success, and >0 if an      error occurs.
 413 
 414 SEE ALSO
 415        awk(1), ed(1), grep(1), regex(5)
 416 
 417 STANDARDS
 418        The sed utility is expected to be a superset of the IEEE Std 1003.2
 419        (``POSIX.2'') specification.
 420 
 421        The -E, I , a and -i options, the prefixing ``+'' in the second member
 422        of an address range, as well as the ``I'' flag to the address regular
 423        expression and substitution command are non-standard extensions and may
 424        not be available on other operating systems.
 425 
 426 HISTORY
 427        A sed command, written by L. E. McMahon, appeared in Version 7 AT&T
 428        UNIX.
 429 
 430 AUTHORS
 431        "Diomidis D. Spinellis" <dds@FreeBSD.org>
 432 
 433 BUGS
 434        Multibyte characters containing a byte with value 0x5C (ASCII `\') may
 435        be incorrectly treated as line continuation characters in arguments to
 436        the ``a'', ``c'' and ``i'' commands.  Multibyte characters cannot be
 437        used as delimiters with the ``s'' and ``y'' commands.
 438 
 439 
 440 
 441                                February 14, 2015                        SED(1)