Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ sed(1) — Reliant UNIX 5.44c4

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

awk(1)

ed(1)

grep(1)

expressions(5)

sed(1)                                                               sed(1)

NAME
     sed - stream editor

SYNOPSIS
     sed [-n] [[-e] script ...] [-f scriptfile ...] [--] [file ...]

DESCRIPTION
     sed is a non-interactive stream editor that provides a similar set of
     functions to the interactive line editor ed.

     sed is a versatile tool that allows you to:

     -  perform multiple global editing functions efficiently in one pass
        through the input,

     -  easily edit piped command output,

     -  apply a sequence of editing commands that is too complicated for
        interactive input.

     sed reads files sequentially, edits each line with sed commands you
     have specified in a sed script, and writes the results on the standard
     output. The sed script is either read straight from the command line
     or taken from a file. sed acts as a filter, i.e. it does not change
     the original input file. If you want to save the changes, you will
     have to redirect the standard output to a file.

OPTIONS
     -n   Suppresses the default output, which is to pass every input line
          to the standard output after processing (see Example 6).

          -n not specified:

          sed copies each processed input line to the standard output, even
          if the sed script does not contain an output command such as p.
          The commands in the sed script determine whether a line is modi-
          fied by editing instructions. Input lines that are processed by
          an output command such as p are thus shown twice in succession.
          First, as part of the default output of all processed lines, and
          then as the output of the special editing command which causes
          them to be displayed again.

     [-e] script
          Uses the command-line script to edit the input file. If the
          script contains blanks, newlines, or shell metacharacters, it
          must be enclosed in single quotes: 'script'.

          The -e script option may be included a number of times with dif-
          ferent scripts and may also be combined with the -f scriptfile
          option. In this case sed applies the commands from all scripts on
          each input line.




Page 1                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

          If the command line contains the -e option only once and does not
          include option -f, you may omit the -e when you specify script.
          According to script, no more options or -- may be specified.

     -f scriptfile
          Reads editing instructions for the input file from the file named
          scriptfile.

          You can include the -f scriptfile option a number of times with
          different script files and also combine it with -e script. sed
          then reads the sed commands from all specified sed scripts.

     --   End of the list of options. Must be specified if file begins with
          - or if script begins with - and -e was not specified.

     file Name of the file whose contents are to be processed by sed. The
          file must be a text file.

          file not specified: sed reads from standard input.

   Functionality

     sed works on copies of the input lines, successively copying each line
     into a temporary work area called a "pattern space".

     Each line of input is normally processed in the following cycle:

     Step 1:     The next (or first) line of input is copied into the pat-
                 tern space.

     Step 2:     All sed script commands that select the pattern space
                 (i.e. address the last line copied into it) are succes-
                 sively applied to its contents. Depending on the editing
                 instruction in the script, the contents of the pattern
                 space are then altered as required, or left unchanged.

     Step 3:     The contents of the edited pattern space are then sent to
                 standard output, and the pattern space is cleared.

     This cycle is repeated until all input lines have been processed.

     The pattern space may occasionally contain multiple lines (see com-
     mands g, G, N). However, it is always the address of the last line
     copied into the pattern space that serves as the pattern space
     address.

     Some sed commands (g, G, h, H) copy text from the pattern space to a
     temporary storage area called the "hold space". The hold space saves
     all or part of the pattern space for subsequent retrieval.





Page 2                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

   Format of a sed script

     A sed script consists of command lines in the form:

     [range]sedcommand[flags ...]

     or

     [range]{sedcommand[flags ...]

              sedcommand[flags ...]

                    .
                    .
                    .

             }

     The braces are required only if you specify an address range.

     The right brace must be positioned at the start of a line, i.e. only
     preceded by blanks or tabs.

     No blanks are permitted between the range and the sedcommand.

     A range serves to select particular input lines and can be specified
     as one or two comma-separated addresses. When the given range
     "selects" the pattern space, i.e. addresses the last line copied to
     it, the associated sed command or command list is applied to the pat-
     tern space.

     range = address
          The pattern space that matches address is selected.

     range = address1,address2
          An inclusive range from the first pattern space that matches
          address1 through the next pattern space that matches address2 is
          selected. However, if address2 is a line number in the input file
          that comes before the one selected with address1, only one line
          is selected (address1).

     range not specified:

     The pattern space is always selected, i.e. the last input line copied
     to it is always addressed.









Page 3                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

   Addresses

     ______________________________________________________________________
    | Address   |  Meaning                                                |
    |___________|_________________________________________________________|
    | $         |  last line of the last input file                       |
    |___________|_________________________________________________________|
    | n         |  nth input line, where n is a positive integer. Input   |
    |           |  lines are numbered consecutively across all files.     |
    |___________|_________________________________________________________|
    | /pattern/ |  Input line containing a string matching the specified  |
    |           |  pattern. Any slash / appearing in pattern must be pre- |
    |           |  ceded by a backslash \ to escape it. pattern is a sim- |
    |           |  ple regular expression [see expressions(5)] with the   |
    |           |  following modifications:                               |
    |           |                                                         |
    |           |  The construction \?regularexpression?, where ? is any |
    |           |  character, is identical to /regularexpression/. Note  |
    |           |  that in the address that in the address, for example,  |
    |           |  the second x is escaped and stands for itself, so that |
    |           |  the regular expression is abcxdef.                     |
    |           |                                                         |
    |           |  The escape sequence \n matches a newline embedded in a |
    |           |  pattern space.                                         |
    |           |                                                         |
    |           |  A period matches any character except the terminal new-|
    |           |  line of the pattern space.                             |
    |||

   sed commands

     The following section contains a list of sed commands, described in
     alphabetical order. The square brackets [ ] are not to be entered;
     they merely indicate that the enclosed address or address range is
     optional.

     The text argument consists of one or more lines. All but the final
     line must end with a backslash \ to escape the terminating newline.

     [address]a\
     text Append text to the pattern space that is output.

     [range]b[label]
          Branch to the sed command :label in the sed script.

          label not specified: Branch to the end of the sed script.








Page 4                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     [range]c\
     text Change. Delete the selected pattern space, send text to the out-
          put and start the next cycle.

     [range]d
          Delete the contents of the pattern space and start the next
          cycle. Step 3 is dropped, i.e. the contents of the pattern space
          are not sent to standard output.

     [range]D
          Delete the initial segment of the pattern space up to (and
          including) the first newline and start the next cycle.

     [range]g
          Replace the contents of the pattern space by the contents of the
          hold space.

     [range]G
          Append the contents of the hold space to the pattern space.

     [range]h
          Replace the contents of the hold space by the contents of the
          pattern space.

     [range]H
          Append the contents of the pattern space to the hold space.

     [address]i\
     text Insert text into the standard output before the contents of the
          pattern space.

     [range]l
          List the pattern space on the standard output, representing non-
          printing characters with replacement characters (e.g. tab charac-
          ters; see ed command l) or with their two-digit octal ASCII code
          equivalents in the form \nn. Long lines with more than 71 charac-
          ters are split into two or more lines (folded). A backslash at
          the end of a screen line indicates that the text line continues
          in the next output line. The end of each line is indicated by $.

     [range]n
          Next. Copy the contents of the pattern space to the standard out-
          put and replace them with the next line of input. The address of
          the last line of input becomes the address of the pattern space.

     [range]N
          Append the next line of input to the pattern space with an embed-
          ded newline. The address of the last appended line becomes the
          address of the pattern space.





Page 5                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     [range]p
          Print the contents of the pattern space on the standard output.
          Non-printing characters are not represented.

     [range]P
          Print the initial segment of the pattern space, up to the first
          newline, on the standard output. Non-printing characters are not
          represented.

     [address]q
          Quit sed. If you have specified multiple sed scripts, sed quits
          at the first q encountered in any of the scripts.

     [range]r rfile
          Read the contents of rfile and send them to the standard output
          before copying the next input line to the pattern space. rfile
          must be separated from the sed command r by exactly one space and
          must come at the end of the command line.

     [range]s/RE/repstring/[flags]
          Substitute repstring for strings that match the regular expres-
          sion RE in the pattern space. RE can be specified in the form of
          a simple regular expression [see expressions(5)]. The delimiter
          does not have to be /: most other characters are accepted. For a
          fuller description see the s command in ed.

          flags

          n    Substitute repstring for just the nth instance of RE in a
               line.

          g    Globally substitute repstring for all instances of RE in a
               line.

          p    Print the contents of the pattern space on the standard out-
               put if a replacement was made. This applies even if sed was
               invoked with the -n option.

          w wfile
               Write. Write the pattern space to the file wfile whenever a
               replacement is made. Any file named wfile that was already
               present before you called sed will be overwritten. If a
               number of w commands in a sed script write to the same
               wfile, the contents of the pattern space will be appended to
               the existing contents of wfile in each case. wfile must be
               separated from the sed command w by exactly one space and
               must come at the end of the command line. A maximum of 10
               different files can be used for wfile in any one invocation
               of sed.

               Caution: If you use the name of your input file as wfile,
                        you will destroy your input file!


Page 6                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

          No flags specified:

          Only the first instance of RE in the line is replaced by rep-
          string.

     [range]tlabel
          Test. Branch to the sed command :label in the sed script if any
          substitutions have been made since the last input line was copied
          to the pattern space or since the most recent execution of a t
          command.

          label not specified: Branch to the end of the sed script.

     [range]w wfile
          Write. Write the pattern space to the file wfile. Any file named
          wfile that was already present before you called sed will be
          overwritten. If a number of w commands in a sed script write to
          the same wfile, the contents of the pattern space will be
          appended to the existing contents of wfile in each case. wfile
          must be separated from the sed command w by exactly one space and
          must come at the end of the command line. A maximum of 10 dif-
          ferent files can be used for wfile in any one invocation of sed.

          Caution: If you use the name of your input file as wfile, you
                   will destroy your input file!

     [range]x
          Exchange the contents of the pattern and hold spaces.

     [range]y/string1/string2/
          Transform. Replace each occurrence of a character in string1 with
          the corresponding character in string2. string1 and string2 must
          be of the same length and must be specified explicitly. Regular
          expressions cannot be used.

     [range]!command
          Don't command. Apply command to all lines not selected by the
          specified address range. command may be any sed command or a sed
          command list enclosed in braces {...}.

     :label
          Set a label for b and t commands to branch to. label is any
          string up to 8 characters long.

     [address]=
          Prints the current line number on the standard output on its own
          line.







Page 7                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     [range]{sedcommand
                 sedcommand
                 .
                 .
                 .
            }

          Successively execute all sed commands enclosed within the braces
          if the address range selects the current pattern space. The right
          brace } must be positioned at the start of a line, i.e. only pre-
          ceded by blanks or tabs.

     <RETURN>
          The newline character is treated as a null command and is
          ignored. This allows you to produce more transparent sed scripts
          by using blank lines.

     #    If # is the first character entered in a line of a script file,
          the entire line is interpreted as a comment.

     #n   If the first line of a script file begins with the character
          sequence #n, the default output of the pattern space is
          suppressed (as with the -n option). The entire line is treated as
          a comment, i.e. not interpreted as an sed command.

ERROR MESSAGES
     sed: command garbled: ...

     The sed script contains a syntax error. The colon is followed by the
     script location at which sed terminated.

     Can't open file

     You have specified a nonexistent input file or a file for which you do
     not have read permission.

     Unrecognized command: ...

     The sed script contains an unknown command.

LOCALE
     The LCMESSAGES environment variable governs the language in which
     message texts are displayed.

     In regular expressions in square brackets, the LCCOLLATE environment
     variable governs the scope of character ranges, equivalence classes
     and collating elements, and the LCTYPE environment variable governs
     the scope of character classes. LCTYPE also governs which characters
     the sed command l treats as non-printing.





Page 8                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     If LCMESSAGES, LCCOLLATE or LCCTYPE is undefined or is defined as
     the null string, it defaults to the value of LANG. If LANG is likewise
     undefined or null, the system acts as if it were not international-
     ized.

     The LCALL environment variable governs the entire locale. LCALL
     takes precedence over all the other environment variables which affect
     internationalization.

     If any of the locale variables has an invalid value, the system acts
     as if none of the variables were set.

EXAMPLES

     Example 1

     Write the string XXXXX in all blank lines of a file and redirect the
     output to another file:

     $ sed '/^$/s/^/XXXXX/' file > newfile

     /^$/ selects all blank lines, i.e. lines that contain nothing (not
     even a blank) between start of line and end of line. sed searches for
     the beginning of the line (^) and replaces it with the string XXXXX.

     Example 2

     Indent by 4 spaces all lines that begin with a digit, and redirect the
     output to another file:

     $ sed '/^[0-9]/s/^/<blank><blank><blank><blank>/' file > newfile

     /^[0-9]/ selects all lines that begin with a digit. sed locates the
     start of the line (^) and replaces it with 4 blanks
     <blank><blank><blank><blank>, i.e. shifts the rest of the line 4 posi-
     tions to the right, padding positions 1 to 4 with spaces.

     Example 3

     Print all the non-blank lines in a file:

     $ sed '/^$/d' file

     All blank lines are selected by /^$/ and deleted by d.

     Example 4

     Make a file double-spaced by adding a blank line after each line in
     the file:

     $ sed 's/$/\ <CR>
     > /' file


Page 9                       Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     Since no address is specified, sed searches for the end of the line
     ($) in every line and replaces it with a newline character <CR>, thus
     adding a blank line after every line.

     Example 5

     Print the second and third column of a file in which individual
     columns are separated from one another by a colon. The third column is
     to be placed before the second:

     $ sed 's/[^:]*:\([^:]*\):\([^:]*\):.*/\2:\1/' file

     Explanation:

     s/   /    /
          The string between the first and second slash is searched for in
          each line and replaced by the string between the second and third
          slash.

     [^:]*
          Any number (*) of characters other than a colon ([^:])

     :    Colon

     \(  \)
          Brackets off part of expression for reuse in the replacement
          string which is between the second and third slash.

     \2   The replacement string is to begin with the part of the expres-
          sion which is in the second bracket \(...\).

     :    There is to be a colon between the first and second part of the
          replacement string.

     \1   The second part of the replacement string is to be the part of
          the expression which is in the first bracket \(...\).


















Page 10                      Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     Example 6

     Filter all professions out of the file personnel and write them into
     the file named professions under a new heading. The following example
     works on the assumption that the used search key (Profession:) does
     not appear in the first line of the file.

     The personnel file has the following format:

     Name: John Miller
     Marital status: Divorced
     Profession : Journalist

     Name: Catherine Baker
     Marital status: Married
     Profession: Programmer
     etc.

     sed program:

     $ sed -n '1{s/^.*/Professions:/
                 h
                }

               /^Profession:/{s/^Profession: *\(.*\)/\1/
                              H
                }

               ${g
                 p
                }' personnel > professions

     $ cat professions
     Professions:
     Journalist
     Programmer
     etc.
     $

     Explanation:

     Line 1 in the pattern space is replaced by the string Professions: and
     then stored in the hold space by the h command.

     Each line in the pattern space that begins with Profession: is
     replaced by the string that follows it and then appended to the hold
     space by the H command.

     The last line of the file ($) is replaced in the pattern space by the
     entire contents of the hold space. Finally the pattern space is
     printed on the standard output by the p command.



Page 11                      Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     The -n option changes the default output so that the input lines
     copied to the pattern space are not automatically sent to standard
     output after editing. Only the p command actually prints the pattern
     space.

     Example 7

     The protocol file has the following contents:

     First Line
     RCS file:        RCS/awk,v;   Working file:    awk
     head:            1.8
     locks:           ;  strict
     symbolic names:  Y4A0010: 1.8;  N4A0010: 1.8;  Y3C1001: 1.7;
     comment leader:  "# "
     total revisions: 8;    selected revisions: 8
     ----------------------------
     Last Line

     A sed script called cutoff can be used

     1) to delete all lines between First Line and Last Line;

     2) to insert a new BEGIN line before First Line;

     3) to add a new END line after Last Line.

     The cutoff script has the following contents:

     #!/usr/bin/ksh
     # Call: cutoff $1

           f=$1
           echo $f

     {
             sed '/^RCS *\(.*\)/,/^----------------------------/d
            /^First Line/i\
     BEGIN
            /^Last Line/a\
     END
          ' $f
             } > $f.out
     mv $f.out $f

     Explanation:

     The

          /^RCS *\(.*\)/,/^----------------------------/d




Page 12                      Reliant UNIX 5.44                Printed 11/98

sed(1)                                                               sed(1)

     statement marks the area to be deleted. It starts with a line begin-
     ning with RCS followed by any sequence of characters [= *\(.*\)]. The
     area to be deleted ends with the line that begins with the character
     string ----------------------------. These two lines as well as all
     lines in between are deleted.

     The

               /^First Line/i\
          BEGIN

     statement inserts a line with the character string BEGIN before a line
     beginning with First Line.

     The

               /^Last Line/a\
          END

     statement adds a line with the character string END after a line
     beginning with Last Line.

     Once the cutoff script has been executed, the protocol file has the
     following contents:

     BEGIN
     First Line
     Last Line
     END

SEE ALSO
     awk(1), ed(1), grep(1), expressions(5).






















Page 13                      Reliant UNIX 5.44                Printed 11/98

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026