Hamilton LaboratoriesHamilton C shell 2012User guideExternal utilities

sed

Oregon Coast

sed
Previous | Next

        Stream editor

Usage:  sed [-hinNs-] [ -f scriptfile ] [ -e script ]
                       [ script ] [ file1 file2 ... ]

   sed is a special text editor for use on streams of data where
   it cycles, reading a line from input, applying the editing
   operations you've specified, and writing the result to stdout.
   The input is read in a single pass and each line is acted on
   only once.

   The editing script can be specified on the command line or,
   if it's long and complex, in a file.  If you want to combine
   a script on the command line with any other script, you must
   use the -e option.  The editing operations available are the
   usual search/replace, insert/delete, etc.  With each
   operation, you generally can specify the lines in the file
   it should affect either by line number or matching a pattern
   or a range of lines.

Options:

   -h              Help.  (This screen.)
   -f scriptfile   Read the script from a file.  Multiple -f
                   options are allowed and the scripts are
                   concatenated.
   -e script       Take the following argument word as a script.
                   Multiple -e options are also allowed.
   -i              Ignore character case.
   -n              Don't automatically write the contents of the
                   edit buffer to stdout at the end of each
                   cycle.
   -N              Normalize any input to use the UNIX convention
                   of a simple \n character to terminate each
                   line, then re-expand the text on output to
                   follow the \r\n convention used on a PC.
                   This option is useful in porting sed scripts
                   from a UNIX system.
   -s              Read the script from stdin.  (The input stream
                   to be edited must be in a file.)
   --              End of options.

Scripts:

   The script is written as a series of commands, each separated
   from the next by a line end or a semicolon.  The format of a
   command is:

      [ address1 [ , address2 ] ] operation

   Spacing between fields is arbitrary:  you can use spaces or
   tabs between fields or have none at all as you prefer.  Here
   are the fields:

      address   is a line number or regular expression to be
                matched.  When a regular expression is used as an
                address, it's normally written as /regexp/ but
                it's also possible to use the syntax \?regexp?
                where ? is any character.

            Zero- or One-Address Operations:

                If you don't give an address, the operation is
                applied to all lines.

                If you give a single address, the operation is
                done on each line that matches.

            Ranges:

                When you give two addresses, you define a range.
                A range can be a single line, e.g., "3,3", or
                a whole group of lines.  If the addresses involve
                pattern matching, e.g.,

                    /^#ifdef/,/^#endif/

                the range might occur over and over in the input
                and will be acted on each time.

                Most operations, e.g., search/replace, are done
                against all lines in the range.  The exceptions
                are i\ (insert), which is acted on at entry to
                the range; a\ (append), and q (quit), which are
                acted on at the end; and c\ (change), which
                deletes all the lines in the range but doesn't
                write any output until the end of the range.

      operation is one of the 28 basic operations provided,
                together with any arguments it takes.

Operations:

   There are two buffers in sed: the edit buffer and the hold
   buffer.  Most of the editing operations work on the edit
   buffer, doing search/replace operations, translating
   characters, and saving or retrieving text in the hold buffer.
   Sed automatically puts each new line into the edit buffer
   (along with its trailing line end) at the start of each cycle
   unless there was something left over after a D (delete head)
   command.

   A second group of operations provide ways of inserting
   blocks of static text.

   The next group of operations provide rudimentary ways of
   condition-testing and branching and of nesting a series of
   operations together.

   Finally there operations for printing and doing other i/o and
   for other miscellaneous things.

Basic Editing:

   d               Delete this section.  Start the next cycle.
   D               Delete from the beginning of edit buffer
                   through and including the first line ending.
                   If there's text remaining, immediately start
                   over at the top of the script without reading
                   a new line.  Otherwise, start the next cycle.

   s/.../.../[ngpw]   Search/replace using regular expressions.
                   In the replace string, "&" means whatever
                   the search string matched. "\n", where n is
                   a number, means whatever matched that tagged
                   expression in the search string.  Other escape
                   sequences, such as "\r" to mean Carriage
                   Return, can also be used, just as they are in
                   a regular expression search string (described
                   below).  The search and replace strings are
                   shown here delimited with /, but you may
                   choose any character you like that doesn't
                   occur in either string.  The following
                   modifiers are accepted:

                   n       A decimal number from 1 to 65,535.
                           Substitute for just the n'th
                           occurrence of the search pattern.
                   g       Global.  Substitute all occurrences.
                   p       Print the edit buffer if a change was
                           made.
                   w file  Write the edit buffer onto the end of
                           the file if a change was made.

   y/.../.../[c]   Translate all occurrences of characters in the
                   first string to the corresponding characters
                   in the second string.  As in the tr utility,
                   ranges are allowed, as in "y/a-z/A-Z/".  The
                   optional "c" flag can be used to complement
                   the input range.  Either string can contain
                   \r and \n characters.  If the first string
                   is longer than the second, all the extra
                   characters are mapped to the last character
                   in the replacement string or to a null if
                   there was none.  If the second string is
                   longer, the extras are ignored.

Using the Hold Buffer:

   g               Get from the hold buffer.  Replace the current
                   contents of the edit buffer.
   G               Get from the hold buffer and paste onto the
                   end of the edit buffer.
   h               Hold.  Replace the contents of the hold buffer
                   with a copy of the edit buffer.
   H               Append to hold.  Copy the edit buffer contents
                   onto the end of the hold buffer.
   x               Exchange the edit and hold buffers.

Inserting blocks of Static Text:

   a\              Append the text to the output at the end
   text            of the cycle if this is the last line in the
                   range.  All but the last line of text should
                   have a "\" just before the \r\n sequence
                   at the end of each line.

   c\              Change this section to read as shown in the
   text            following text.  Start the next cycle.

   i\              Insert.  Immediately copy the following text
   text            to stdout if this is the start of the range.

Condition-Testing, Branching and Grouping operations:

   b label         Branch to the label elsewhere in the script.
                   (If no label is given, it's to the end of the
                   script.)
   q               If this is the end of the range, quit.
   t label         Branch to the label if search/replace changes
                   have been made since the most recent input
                   line was read or a t operation was run.
   : label         Label.  (No address prefix is allowed.)
   {  }            Group a series of operations together.

Printing:

   =               Write the line number to stdout.
   l               List the contents of the edit buffer in C
                   language style with escape sequences for
                   binary characters.
   p               Print.  Write the contents of the edit buffer
                   to stdout.
   P               Print from the beginning of the edit buffer
                   through and including the first line ending.
   #n              Suppress any automatic output at the end of
                   each cyle.

Other I/O operations:

   n               Next line.  Write the contents of the edit
                   buffer to stdout, dump any appended text and
                   read a new line from stdin to the edit buffer.
   N               Read the next line onto the end of the edit
                   buffer with a \r\n sequence in between.
   r file          Copy the contents of this file to stdout at
                   the end of the cycle.
   w file          Write the edit butter onto the end of the
                   file, creating it if it doesn't exist.

Miscellaneous:

   ! operation     Don't apply this function unless the
                   addressing DOESN'T match.  Invert the line
                   selections.
   ;               Null statement.
   # comment       Comments and blank lines are ignored.

   If multiple a\ (append) or r (read file) instructions are
   executed (or the same one is iterated in a loop), a new entry
   is made on the end of a list of all the appended text blocks
   to be copied to stdout at the end of the cycle.  When the end
   of the cycle is finally reached, the whole list is dumped,
   in order from the top.

Regular Expressions:

   Search patterns are specified as regular expressions like
   those used by grep.  Regular expressions are written in this
   notation, in decreasing precedence:

      c        Any ordinary character matches itself.

      \c       Match the literal character c.  Certain
               characters are treated specially:

                 \a  Audible Alert (Bell)    \r  Carriage Return
                 \b  BackSpace               \t  Tab
                 \f  Form Feed               \v  Vertical Tab
                 \n  NewLine                 \\  Single BackSlash

                 \x  The next one or two characters are treated
                     as hex digits specifying the character code.

      ^        Beginning of line.
      $        End of line.
      .        Match any single character.
      [...]    Match any single character in the list.
      [^...]   Match any single character not in the list.
      \n       Match whatever literal text the n'th tagged
               \(...\) expression matched.
      r*       Match zero or more occurrences of r.
      r\{n\}   Match exactly n occurrences of r, where n is an
               unsigned decimal integer.
      r\{n,\}  Match at least n occurrences of r.
      r\{n,m\} Match at least n, but not more than m occurrences
               of r.
      r\{,m\}  Match at most m occurrences of r.
      r1r2     Match expression r1 followed by r2.
      \(r\)    Tagged regular expression.  Match the pattern
               inside the \(...\), and remember the literal
               text that matched.

   A regular expression pattern cannot contain Nulls but it can
   contain NewLine or CarriageReturn characters (which may be
   useful with the N or G commands.)

   When typing a regular expression on the command line, remember
   that $, [, ], ^, ( and ) have special meaning to Hamilton C
   shell.  Put single quotes around the string to turn off that
   special meaning.  Also, even inside quotes, type ^^ to mean ^
   except when it immediately follows [.

   Also, / at the beginning of a word on the command line is
   taken as introducing an option.  If you mean it to be the
   start of a script, use the "--" option ahead of it.

Previous | Next