ed(1)									ed(1)



NAME

  ed, e, red - Edits a file line by line

SYNOPSIS

  ed [-p string] [-s] [file]

  e [-p string] [-s] [file]

  red [-p string] [-s] [file]

  The ed command is a line editing program that works on one file at a time
  by copying it into a temporary edit buffer and making changes to that copy.

FLAGS

  -p string
      Sets the ed prompt to string.  The default for string is null (no
      prompt).

  -s  Suppresses byte counts that the editor displays with the e, E, r, and w
      subcommands, suppresses diagnostic messages for the e, E, and q subcom-
      mands, and suppresses the ! (exclamation point) prompt after a
      !system_command.	This also holds true if you invoke ed with a
      filename.

      Note that if you quit without writing the file, you do not get an error
      message.	(Generally, before ending the program, q checks to determine
      whether the buffer was written to a file since the last time it was
      changed.	If not, q displays the ? message.)

DESCRIPTION

  The ed command does not alter the file itself until you use the write sub-
  command.  You can specify on the command line the file you want to edit, or
  you can use ed subcommands to read a file into the buffer.  When ed reads a
  new file into the buffer, the contents of that file replace the buffer's
  previous contents, if any.

  There is also a restricted version of ed, the red command, for use with the
  restricted shell (rsh).  With red, you can edit only files that reside in
  the current directory, or in the /tmp directory, and you cannot use the
  !system_command subcommand (see SUBCOMMANDS).

  The ed Modes


  The ed program operates in one of two modes, command mode and text mode.
  In command mode, ed recognizes and executes subcommands.  In text mode, ed
  adds text to the file buffer, but does not recognize subcommands.  You
  enter text mode by using the a subcommand, c subcommand, or i subcommand.
  To leave text mode, enter . (dot) alone at the beginning of a line.

  The e command is a supported alias for the ed command.




  Subcommand Syntax


  An ed subcommand consists of zero, one, or two addresses, followed by a
  single-character subcommand, possibly followed by arguments to that subcom-
  mand.	 These addresses specify one or more lines in the buffer.  Because
  every subcommand has default addresses, you frequently do not need to
  specify addresses.

  Pattern Matching


  The ed command supports a limited form of special pattern-matching charac-
  ters that you can use as regular expressions (REs) to construct pattern
  strings.  You can use these patterns in addresses to specify lines and in
  some subcommands to specify portions of a line.

  For information about regular expressions (REs), see the grep command.

  Addressing


  There are three types of ed addresses:  line number addresses, addresses
  relative to the current line, and pattern addresses.

  The current line is the point of reference in the buffer and is represented
  by a . (dot).	 When you start the ed program, the current line is the last
  line in the buffer.  As you edit a file, the current line changes to the
  last line affected by a subcommand.  The current line is the default
  address for several ed commands.  (See SUBCOMMANDS to find out how each
  subcommand affects the current line.)

  Subcommands for determining and changing the current line are described in
  the section SUBCOMMANDS.  The following are guidelines for constructing
  addresses:

    +  . (dot) addresses the current line.

    +  $ addresses the last line of the buffer.

    +  n addresses the nth line of the buffer.

    +  'x addresses the line marked with a lowercase letter, x, by the k sub-
       command.

    +  The /pattern/ construct addresses the next line that contains a match-
       ing string.  The search begins with the line after the current line
       and stops when it finds a match for the pattern.	 If necessary, the
       search moves to the end of the buffer, wraps around to the beginning
       of the buffer, and continues until it either finds a match or returns
       to the current line.

    +  The ?pattern? construct addresses the previous line that contains a
       match for the pattern.  The ?pattern? construct, like /pattern/, can
       search the entire buffer, but it does so in the opposite direction.

    +  An address followed by +number or -number specifies an address plus or
       minus the indicated number of lines.  (The + (plus sign) is optional.)

    +  An address that begins with + or - specifies a line relative to the
       current line.  For example, -5 is the equivalent of .-5 (five lines
       above the current line).

    +  An address that ends with - or + specifies the line immediately before
       (-) or immediately after (+) the addressed line.	 Used alone, the -
       character addresses the line immediately before the current line.  The
       + character addresses the line immediately after the current line;
       however, the + character is optional.  The + and - characters have a
       cumulative effect; for example, the address -- addresses the line two
       lines above the current line.

    +  For convenience, a , (comma) stands for the address pair 1,$ (first
       line through last line) and a ; (semicolon) stands for the pair .,$
       (current line through last line).

  Commands that do not accept addresses regard the presence of an address as
  an error.  Commands that do accept addresses can use either given or
  default addresses.  When given more addresses than it accepts, a command
  uses the last (rightmost) ones.

  In most cases, a , (comma) separates addresses (for example 2,8).  A ;
  (semicolon) can also separate addresses.  A ; between addresses causes ed
  to set the current line to the first address and then calculate the second
  address (for example, to set the starting line for a search based on guide-
  lines 5 and 6).  In a pair of addresses, the first must be numerically
  smaller than the second.

  The ed Limits


  The following is a list of ed size limitations:

    +  64 bytes per filename.

    +  512 bytes per line.

    +  256 bytes per global subcommand list.

    +  128 kilobyte buffer size.  (Note that the buffer not only contains the
       original file, but also editing information.  Each line occupies one
       word in the buffer.)

  The maximum number of lines depends on the amount of memory available to
  you.	The maximum file size depends on the amount of physical data storage
  (disk or tape drive) available or on the maximum number of lines permitted
  in user memory.

SUBCOMMANDS

  In most cases, only one ed subcommand can be entered on a line.  The excep-
  tions to this rule are the n, p, and l subcommands, which can be added to
  any subcommand except e, E, f, q, Q, r, w, or !.

  The e, E, f, r, and w subcommands accept filenames as arguments.  The ed
  command stores the last filename used with a subcommand as a default
  filename.  The next e, E, f, r, or w given without a filename uses the
  default filename.

  The ed command responds to an error condition with one of two messages:  ?
  (question mark) or ?file.  When the ed command receives an INT signal, it
  displays a ? and returns to command mode.  When ed reads a file, it dis-
  cards NULL characters and all characters after the last newline character.

  Unless otherwise noted, all subcommands work by default on the current
  line; an address is optional.	 If you specify an address, you do not type
  the brackets.

  When used as an address, a . (dot) refers to the current line.  When a .
  (dot) is shown in the first position on an otherwise blank line, it ter-
  minates text mode and returns to command mode.  text denotes user input in
  text mode.  Note that address need not be a number; it can be a regular
  expression of the form /RE/,/RE/ or /RE/;/RE/.

  [address]a

  text

  .   The a (append) subcommand adds text to the buffer after the addressed
      line.  Enter a . (dot) to return to command mode.	 The a subcommand
      sets the current line to the last inserted line, or, if no lines were
      inserted, to the addressed line.	Address 0 causes the a subcommand to
      add text to the beginning of the buffer.

  [address1,address2]c

  text

  .   The c (change) subcommand deletes the addressed lines, then replaces
      them with new input.  Enter a . (dot) to return to command mode.	The c
      command sets the current line to the last new line of input, or, if
      there were none, to the first line that was not deleted.

  [address1,address2]d
      The d (delete) subcommand removes the addressed lines from the buffer.
      The line after the last line deleted becomes the current line.  If the
      deleted lines were originally at the end of the buffer, the new last
      line becomes the current line.

  e file
      The e (edit) subcommand first deletes any contents from the buffer,
      then loads another file into the buffer, sets the current line to the
      last line of the buffer, and displays the number of bytes read in to
      the buffer.  If the buffer was changed since its contents were last
      saved (with the w subcommand), e displays ? before it clears the
      buffer.

      The e subcommand stores file as the default filename to be used, if
      necessary, by subsequent e, E, r, or w subcommands.  (See the f subcom-
      mand.)

      When the ! (exclamation point) character replaces file, e takes the
      rest of the line as a shell (sh) command and reads the command output.
      The e subcommand does not store the name of the shell command as a
      default filename.

  E file
      The E (Edit) subcommand works like e, with one exception:	 E does not
      check for changes made to the buffer since the last w subcommand.

  f [file]
      The f (file name) subcommand changes the default filename (the stored
      name of the last file used) to file, if file is given.  If file is not
      given, the f subcommand prints the default filename.

  [address1,address2]g/pattern/subcommand_list
      The g (global) subcommand first marks every line that matches the pat-
      tern.  Then, for each marked line, this subcommand sets the current
      line to that line and executes subcommand_list.  Place a single subcom-
      mand, or the first subcommand of a list, on the same line with the g
      subcommand; place subsequent subcommands on separate lines.  Except for
      the last line, end each of these lines with a \ (backslash).

      The subcommand_list can include the a, i, and c subcommands and their
      input.  If the last command in subcommand_list is normally the . (dot)
      that ends input mode, the . (dot) is optional.  If there is no
      subcommand_list, ed displays the current line.  The subcommand_list
      cannot include the g, G, v, V, or ! subcommands.

      The g subcommand is similar to the v subcommand, which executes
      subcommand_list for every line that does not contain a match for the
      pattern.	Note that this subcommand defaults to the entire file, not to
      the current line.

  [address1,address2]G/pattern/
      The interactive G (Global) subcommand first marks every line that
      matches the pattern, then displays the first marked line, sets the
      current line to that line, and waits for a subcommand.  The G subcom-
      mand accepts any but the following ed subcommands:  a, c, i, g, G, v,
      V, and !.	 After the subcommand finishes, G displays the next marked
      line, and so on.	The G subcommand takes a newline character as a null
      subcommand.  The & subcommand causes G to execute the previous subcom-
      mand again, if there is one.  Note that subcommands executed within the
      G subcommand can address and change any lines in the buffer.  The G
      subcommand can be terminated by pressing the Interrupt key sequence.
      Note that this subcommand default to the entire file, not to the
      current line.

  h   The h (help) subcommand displays a short message that explains the rea-
      son for the most recent ? notification.  The current line number is
      unchanged.

  H   The H (Help) subcommand causes ed to enter a mode in which help mes-
      sages (see the h command) are displayed for all subsequent ? notifica-
      tions.  The H subcommand toggles this mode, and is initially set to
      "off".  The current line number is unchanged.

  [address]i

  text

  .   The i (insert) subcommand inserts text before the addressed line and
      sets the current line to the last inserted line.	Enter . (dot) to
      return to command mode.  If no lines are inserted, i sets the current
      line to the addressed line.  This subcommand differs from the a subcom-
      mand only in the placement of the input text.  Address 0 is not legal
      for this subcommand.

  [address1,address1+1]j
      The j (join) subcommand joins contiguous lines by removing the inter-
      vening newline characters.  If given only one address, j does nothing.
      (For splitting lines, see the s subcommand.) Note that lines that
      exceed the line length limit cannot be joined.  If lines are joined,
      the current line number is set to the address of the joined line; oth-
      erwise, the current line number is unchanged.

  [address]kx
      The k (mark) subcommand marks the addressed line with name x, which
      must be a lowercase letter.  The address 'x (single quotation mark
      before the marking character) then addresses this line.  The k subcom-
      mand does not change the current line.  Note that marks attached to
      lines are deleted with the line.

  [address1,address2]l
      The l (list) subcommand displays the addressed lines.  The l subcommand
      wraps long lines and, unlike the p subcommand, represents nonprinting
      characters as 3-digit octal numbers with a \ (backslash) preceding each
      byte in the character.  The following characters, however, are written
      as escape sequences:

      \\  Backslash

      \a  Alert

      \b  Backspace

      \f  Formfeed

      \n  Newline

      \r  Carriage return

      \t  Tab

      \v  Vertical tab

      A $ (dollar sign) character is placed at the end of each line, so that
      a real (literal) $ at the end of a line cannot be misinterpreted.

      An l subcommand can be appended to any ed subcommand except: e, E, f,
      q, Q, r, w, or !.

  [address1,address2]maddress3
      The m (move) subcommand repositions the addressed lines.	The first
      moved line follows the line addressed by address3.  Address 0 for
      address3 causes m to move the addressed lines to the beginning of the
      file.  The line specified by address3 cannot be one of the lines to be
      moved.  The m subcommand sets the current line to the last moved line.

  [address1,address2]n
      The n (number) subcommand displays the addressed lines, each preceded
      by its line number and a tab character (displayed as spaces); the n
      subcommand leaves the current line at the last line displayed.  An n
      subcommand can be appended to any ed subcommand except e, E, f, q, Q,
      r, w, or !.

  [address1,address2]p
      The p (print) subcommand displays the addressed lines and sets the
      current line set to the last line displayed.  A p subcommand can be
      appended to any ed subcommand except: e, E, f, q, Q, r, w, or !.	For
      example, the subcommand dp deletes the current line and displays the
      new current line.

  P   The P (Prompt) subcommand turns the ed prompt string * or the string
      specified by the -p flag on or off.  Initially, P is off.

  q   The q (quit) subcommand exits the ed program.  Before ending the pro-
      gram, q checks to determine whether the buffer was written to a file
      since the last time it was changed.  If not, q displays the ? message.
      Note that you do not get more than one prompt in a row; a second con-
      secutive q quits the ed program without displaying a prompt.

  Q   The Q (Quit) subcommand exits the ed program without checking for
      changes to the buffer since the last w subcommand (compare with the q
      subcommand).

  [address]r file
      The r (read) subcommand reads a file into the buffer after the
      addressed line; r does not delete the previous contents of the buffer.
      When entered without file, r reads the default file, if any, into the
      buffer (see e and f subcommands).	 The r subcommand does not change the
      default filename.	 Address 0 causes r to read a file in at the begin-
      ning of the buffer.  After it reads a file successfully, r, displays
      the number of bytes read into the buffer and sets the current line to
      the last line read.

      If ! (exclamation point) replaces file in a r subcommand, r takes the
      rest of the line as a shell (sh) command whose output is to be read.
      The r subcommand does not store the names of shell commands as default
      filenames.

  [address1,address2]s/pattern/replacement/flags
      The s (substitute) subcommand searches each addressed line for a string
      that matches the pattern and then replaces the string with the speci-
      fied replacement string.	Without a number n or the global indicator g,
      s replaces only the first matching string on each addressed line.	 With
      n, s replaces the nth occurrence of pattern on the addressed line.
      With the g indicator, s replaces every occurrence of the matching
      string on each addressed line.

      If s does not find a match for the pattern, it returns the error mes-
      sage ?.  Any character except a space or a newline character can
      separate (delimit) the pattern and replacement.  The s subcommand sets
      the current line to the last line changed.

      An & (ampersand) in the replacement string is a special symbol that has
      the same value as the pattern string.  So, for example, the subcommand
      s/out/&ing/ has the same effect as the subcommand s/out/outing/ and
      replaces out with outing on the current line.  A backslash before the
      ampersand (\&) removes this special meaning of & in replacement.

      The % (percent sign), when used by itself as replacement, causes s to
      use the previous replacement again.  The % character does not have this
      special meaning if it is part of a longer replacement or if it is pre-
      ceded by a \ (backslash).

      Lines can be split by substituting newline characters into them.	In
      replacement, the sequence \ quotes the newline character (not
      displayed) and moves the cursor to the nextline for the remainder of
      the string.

      The value of flags can be the following:

      count
	  Substitutes for the countth occurrence only of the regular expres-
	  sion that is found on each addressed line.

      g	  Substitutes globally for all nonoverlapping instances of the regu-
	  lar expression, instead of just substituting for the first
	  instance.

      l	  Displays the final line in which a substitution was made in the
	  format specified for the l subcommand.

      n	  Displays the final line in which a substitution was made in the
	  format specified for the n subcommand.

      p	  Displays the final line in which a substitution was made in the
	  format specified for the p subcommand.

  [address1,address2]taddress3
      The t (transfer) subcommand inserts a copy of the addressed lines after
      address3.	 The t subcommand accepts address 0 (for inserting lines at
      the beginning of the buffer).  The t subcommand sets the current line
      to the last line copied.

  u   The u (undo) subcommand restores the buffer to the state it was in
      before it was last modified by an ed subcommand.	The commands that u
      can undo are a, c, d, g, G, i, j, m, r, s, t, u, v, and V.  All changes
      made to the buffer by a g, G, v, or V global command are undone as a
      single change.  The current line number is set to the value it had
      before the command being undone started.

  [address1,address2]v/pattern/subcommand_list
      The v subcommand executes the subcommands in subcommand_list for each
      line that does not contain a match for the pattern.  The v subcommand
      is a complement for the global subcommand g, which executes
      subcommand_list for every line that does contain a match for the pat-
      tern.

  [address1,address2]V/pattern/
      The V subcommand first marks every line that does not match the pat-
      tern, then displays the first marked line, sets the current line to
      that line, and waits for a subcommand.  The V subcommand complements
      the G subcommand, which marks the lines that do match the pattern.

  [address1,address2]w file
      The w (write) subcommand copies the addressed lines from the buffer to
      the file named in file.  If the file does not exist, the w subcommand
      creates it with permission mode 666 (read and write permission for
      everyone), unless the umask setting specifies another file creation
      mode.  (For information about file permissions, see umask in sh and
      chmod.)

      The w subcommand does not change the default filename (unless file is
      the first filename used since you started ed).  If you do not provide a
      filename, ed uses the default filename, if any (see the e, E, and f
      subcommands).  The w subcommand does not change the current line.

      If ed successfully writes the file, it displays the number of charac-
      ters written.  When ! (exclamation point) replaces file, ed takes the
      rest of the line as a shell (sh) command whose output is to be read; w
      does not save shell command names as default filenames (same effect as
      !).  The use of the write subcommand with ! (exclamation point) is not
      considered the last w command that wrote the entire buffer.  Thus, this
      alone does not prevent the warning to the user if an attempt is made to
      destroy the editor buffer through the e or q subcommands.

      The address 0 is not a legal address for the w subcommand.  Therefore,
      it is not possible to create an empty file with ed.

  [address]=
      Without an address, the = (equal sign) subcommand displays the current
      line number.  With the address $, = (equal sign) displays the number of
      the last line in the buffer.  The = subcommand does not change the
      current line and cannot be included in a g or v subcommand list.

  !system_command
      The ! (exclamation point) subcommand allows system commands to be run
      from within ed.  Anything following ! on an ed subcommand line is
      interpreted as a system command.	Within the text of that command
      string, ed replaces the unescaped character %with the current filename,
      if there is one.

      By default, the subcommands are executed by /usr/bin/sh. To support
      single user mode operation, subcommands can be executed by /sbin/sh. To
      change the default behavior, modify your $SHELL environmental variable
      to SHELL=/sbin/sh.

      When used as the first character of a shell command (after the ! that
      runs a subshell), ed replaces the ! character with the previous system
      command; for example, the command !! repeats the previous system com-
      mand.  If the command interpreter (the sh command) expands the command
      string, ed echoes the expanded line.  The ! subcommand does not change
      the current line.	 If any replacements of % and/or ! are performed, the
      modified line is written to standard output before the command is exe-
      cuted.

  number

  +number

  -number
      The ed editor interprets a number alone on a line as an address and
      displays the addressed line.  Addresses can be absolute (line numbers
      or $) or relative to the current line (+number or -number).  Entering a
      newline character (a blank line) is equivalent to +1p and is useful for
      stepping forward through the buffer one line at a time.

EXIT VALUES

  The ed editor returns a value of 0 (zero) if execution is successfully com-
  pleted; if an error occurs, a value greater than 0 (zero) is returned.

RELATED INFORMATION

  Commands:  chmod(1), edit(1), ex(1), grep(1)/egrep(1)/fgrep(1), rsh(1),
  sed(1), sh(1), stty(1), vi(1)/vedit(1)/view(1).

  Functions:  regexp(3).

  Files:  locale(4).

  Books: Command and Shell User's Guide