Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ sed(1) — AIX/RT 2.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

awk

ed

grep

sed

PURPOSE

     Provides a stream editor.

SYNOPSIS
     sed [ -n ] [ -e script ] [ -f sfile ] [ files ]


DESCRIPTION

     The sed  command modifies  lines from the  specified file
     according to an  edit script and writes  them to standard
     output.   The  sed  command includes  many  features  for
     selecting lines to be modified and making changes only to
     the selected lines.

     The sed command uses two work spaces for holding the line
     being modified:   the pattern  space, where  the selected
     line is  held, and the  hold space,  where a line  can be
     stored temporarily.

     An edit  script consists of individual  subcommands, each
     one on a separate line.   The general form of sed subcom-
     mands is:

         [address-range] function[modifiers]

     The sed command  processes each input file  by reading an
     input line into a pattern space, applying all sed subcom-
     mands in  sequence whose addresses select  that line, and
     writing the  pattern space  to standard output.   It then
     clears  the pattern  space and  repeats this  process for
     each line in the input file.  Some of the subcommands use
     a hold space to save all or part of the pattern space for
     subsequent retrieval.

     When a command includes an  address, either a line number
     or a search pattern, only  the addressed line or lines is
     affected  by  the  command.  Otherwise,  the  command  is
     applied to all lines.

     An address is  either a decimal line number,  a $ (dollar
     sign),  which addresses  the  last line  of  input, or  a
     context  address.    A  context  address  is   a  regular
     expression similar  to those  used in  ed except  for the
     following differences:

     o   You can select the  character delimiter for patterns.
         The general form of the expression is:

           \?pattern?

         where ?  is a  character delimiter you  select.  This
         delimiter  cannot be  a two-byte  international char-
         acter support  extended character.  The  default form
         for the pattern is:

           /pattern/

     o   The sequence  \n matches a new-line  character in the
         pattern space, except the terminating new line.
     o   A . (dot) matches  any character except a terminating
         new-line character.  That is, unlike ed, which cannot
         match a new-line  character in the middle  of a line,
         sed  can match  a new-line  character in  the pattern
         space.

     Certain commands allow you to specify one line or a range
     of lines to  which the command should  be applied.  These
     commands  are called  addressed commands.   The following
     rules apply to addressed commands:

     o   A command line with no address selects every line.
     o   A command line with one address, expressed in context
         form, selects each line that matches the address.
     o   A command line with two addresses separated by commas
         selects  the entire  range from  the first  line that
         matches the first address  through the next line that
         matches  the second.   (If  the second  address is  a
         number less  than or equal  to the line  number first
         selected, only one line is selected.)  Thereafter the
         process  is repeated,  looking  again  for the  first
         address.

     Notes:

     1.  The text  parameter accompanying  the a\, c\,  and i\
         commands can  continue onto  more than one  line pro-
         vided all  lines but the last  end with a \  to quote
         the  new-line  character.   Backslashes in  text  are
         treated like backslashes in the replacement string of
         an  s command  and  can be  used  to protect  initial
         blanks and tabs against the stripping that is done on
         every script  line.  The  rfile and  wfile parameters
         must end  the command  line and  must be  preceded by
         exactly  one blank.   Each  wfile  is created  before
         processing begins.

     2.  The sed  command can process  up to 99 commands  in a
         pattern file.

FLAGS

     -e  "script"  Uses the file script as the editing script.
                   If you are using just one -e flag and no -f
                   flag.  the -e flag may be omitted.
     -f  sfile     Uses  sfile  as  the  source  of  the  edit
                   script.  sfile is a prepared set of editing
                   commands to be applied to file.
     -n            Suppresses all information normally written
                   to standard output.

SUBCOMMANDS

     In the following list of functions, the maximum number of
     permissible addresses  for each function is  indicated in
     parentheses.  The sed script subcommands are as follows:

     (1) a\
     text         Places text on the output before reading the
                  next input line.

     (2)b[label]  Branches to the : command bearing the label.
                  If label is empty, it branches to the end of
                  the script.

     (2)c\
     text         Deletes  the pattern  space.   With  0 or  1
                  address or at the  end of a 2-address range,
                  places text on the  output.  Starts the next
                  cycle.

     (2)d         Deletes the pattern  space.  Starts the next
                  cycle.
     (2)D         Deletes the  initial segment of  the pattern
                  space through the  first new-line character.
                  Starts the next cycle.
     (2)g         Replaces the  contents of the  pattern space
                  by the contents of the hold space.
     (2)G         Appends the  contents of  the hold  space to
                  the pattern space.
     (2)h         Replaces the  contents of the hold  space by
                  the contents of the pattern space.
     (2)H         Appends the contents of the pattern space to
                  the hold space.

     (1)i\
     text         Writes  text   to  standard   output  before
                  reading  the  next  line  into  the  pattern
                  space.

     (2)l         Writes the pattern  space to standard output
                  showing  nondisplayable  characters as  two-
                  digit octal values.  Long lines are folded.
     (2)n         Writes the pattern space to standard output.
                  Replaces  the pattern  space  with the  next
                  line of input.
     (2)N         Appends  the  next  line  of  input  to  the
                  pattern  space  with  an  embedded  new-line
                  character.    (The   current   line   number

                  changes.)  You  can use  this to  search for
                  patterns that may be split onto two lines.
     (2)p         Writes the pattern space to standard output.
     (2)P         Writes  the initial  segment of  the pattern
                  space through  the first  new-line character
                  to standard output.
     (1)q         Branches to the end of the script.  Does not
                  start a new cycle.
     (2)r rfile   Reads  the contents  of rfile.   Places con-
                  tents on the output  before reading the next
                  input line.
     (2)s/pattern/replacement/flags
                  Substitutes the  replacement string  for the
                  first  occurrence  of  the  pattern  in  the
                  pattern  space.    Any  character  appearing
                  after the  s can substitute for  the / sepa-
                  rator.

                  You can  add zero  or more of  the following
                  flags:
                  g  Substitutes all  nonoverlapping instances
                     of the pattern rather than just the first
                     one.
                  p  Writes the pattern  space to standard out
                     if a replacement was made.
                  w wfile
                     Writes the  pattern space  to wfile  if a
                     replacement   was   made.   Appends   the
                     pattern space to wfile.  If wfile was not
                     already  created by  a previous  write by
                     this sed script, sed creates it.
     (2)tlabel    Branches to :label in the script file if any
                  substitutions  were  made   since  the  most
                  recent reading of an input line execution of
                  a  t  subcommand.   If you  do  not  specify
                  label, control  transfers to the end  of the
                  script.
     (2)wwfile    Appends the pattern space to wfile.
     (2)x         Exchanges the contents  of the pattern space
                  and the hold space.
     (2)y/pattern1/pattern2/
                  Replaces  all occurrences  of characters  in
                  pattern1  with the  corresponding characters
                  pattern2.  The byte  lengths of pattern1 and
                  pattern2 must be equal.
     (2)!sed-cmd  Applies the specified sed subcommand only to
                  lines  not   selected  by  the   address  or
                  addresses.
     (0):label    This  script  entry  simply marks  a  branch
                  point to be  referenced by the b  and t com-
                  mands.  This  label can  be any  sequence of
                  eight or fewer bytes.
     (1)=         Writes the  current line number  to standard
                  output as a line.

     (2){subcmd
                  .

            }     Groups subcommands enclosed in {} (braces).

EXAMPLES

     1.  To perform a global change:

           sed  "s/happy/enchanted/g"  chap1  >chap1.new

         This replaces each occurrence of "happy" found in the
         file "chap1"  with "enchanted",  and puts  the edited
         version in a separate  file named "chap1.new".  The g
         at the end  of the s subcommand tells sed  to make as
         many substitutions as possible on each line.  Without
         the g, sed replaces only the first "happy" on a line.

         The sed stream editor operates as a filter.  It reads
         text from standard  input or from the  files named on
         the command line ("chap1"  in this example), modifies
         this text, and writes  it to standard output.  Unlike
         most editors, it does  not replace the original file.
         This makes sed a powerful  command when used in pipe-
         lines.
     2.  To use sed as a filter in a pipeline:

           pr  chap2 | sed  "s/Page  *[0-9]*$/(&)/" | print

         This encloses the page  numbers in parentheses before
         printing "chap2".  The pr  command puts a heading and
         page number  at the top  of each page, then  sed puts
         the  page  numbers  in  parentheses,  and  the  print
         command prints the edited listing.

         The  sed   pattern  "/Page *[0-9]*$/"   matches  page
         numbers that appear at the end of a line.  The s sub-
         command changes this to  "(&)", where the "&" (amper-
         sand) stands for the page number that was matched.
     3.  To display selected lines of a file:

           sed  -n  "/food/p" chap3

         This displays each line  in "chap3" that contains the
         word  "food".  Normally,  sed  copies  every line  to
         standard  output after  it  is edited.   The -n  flag
         stops sed from doing  this.  You then use subcommands
         like p to write specific  parts of the text.  Without
         the -n, this  example would display all  the lines in
         "chap3",  and  it  would show  each  line  containing
         "food" twice.
     4.  To perform complex editing:

           sed  -f  script.sed  chap4  >chap4.new

         It is always a good idea  to create a sed script file
         when you want  to do anything very  complex.  You can
         then  test and  modify your  script before  using it.

         You can also  reuse your script to  edit other files.
         Create  the  script  file with  an  interactive  text
         editor.
     5.  A sample sed script file:

           :join
           /\\$/{N
           s/\\\n//
           b join
           }

         This sed  script joins each  line that ends with  a \
         (backslash) to the line  that follows it.  First, the
         pattern "/\\$/" selects a line that ends with a \ for
         the group of commands enclosed  in "{ }".  The N sub-
         command then appends the  next line, imbedding a new-
         line  character.  The  "s/\\\n//" deletes  the \  and
         imbedded   new-line  character.    Finally,  b" join"
         branches back to  the label ":join" to check  for a \
         at the  end of  the newly  joined line.   Without the
         branch, sed writes the joined  line and read the next
         one before checking for a second \.

         Note:   The N  subcommand  causes sed  to stop  imme-
         diately if there are no more lines of input (that is,
         if N  reads the end-of-file character).   It does not
         copy  the pattern  space  to  standard output  before
         stopping.  This  means that if  the last line  of the
         input ends  with a \,  then it  is not copied  to the
         output.

RELATED INFORMATION

     The following commands:  "awk,"  "ed," and "grep."

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