DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(sed.info.gz) The "s" Command

Info Catalog (sed.info.gz) Common Commands (sed.info.gz) sed Programs (sed.info.gz) Other Commands
 
 The `s' Command
 ===============
 
    The syntax of the `s' (as in substitute) command is
 `s/REGEXP/REPLACEMENT/FLAGS'.  The `/' characters may be uniformly
 replaced by any other single character within any given `s' command.
 The `/' character (or whatever other character is used in its stead)
 can appear in the REGEXP or REPLACEMENT only if it is preceded by a `\'
 character.
 
    The `s' command is probably the most important in `sed' and has a
 lot of different options.  Its basic concept is simple: the `s' command
 attempts to match the pattern space against the supplied REGEXP; if the
 match is successful, then that portion of the pattern space which was
 matched is replaced with REPLACEMENT.
 
    The REPLACEMENT can contain `\N' (N being a number from 1 to 9,
 inclusive) references, which refer to the portion of the match which is
 contained between the Nth `\(' and its matching `\)'.  Also, the
 REPLACEMENT can contain unescaped `&' characters which reference the
 whole matched portion of the pattern space.  Finally, as a GNU `sed'
 extension, you can include a special sequence made of a backslash and
 one of the letters `L', `l', `U', `u', or `E'.  The meaning is as
 follows:
 
 `\L'
      Turn the replacement to lowercase until a `\U' or `\E' is found,
 
 `\l'
      Turn the next character to lowercase,
 
 `\U'
      Turn the replacement to uppercase until a `\L' or `\E' is found,
 
 `\u'
      Turn the next character to uppercase,
 
 `\E'
      Stop case conversion started by `\L' or `\U'.
 
    To include a literal `\', `&', or newline in the final replacement,
 be sure to precede the desired `\', `&', or newline in the REPLACEMENT
 with a `\'.
 
    The `s' command can be followed by zero or more of the following
 FLAGS:
 
 `g'
      Apply the replacement to _all_ matches to the REGEXP, not just the
      first.
 
 `NUMBER'
      Only replace the NUMBERth match of the REGEXP.
 
      Note: the POSIX standard does not specify what should happen when
      you mix the `g' and NUMBER modifiers, and currently there is no
      widely agreed upon meaning across `sed' implementations.  For GNU
      `sed', the interaction is defined to be: ignore matches before the
      NUMBERth, and then match and replace all matches from the NUMBERth
      on.
 
 `p'
      If the substitution was made, then print the new pattern space.
 
      Note: when both the `p' and `e' options are specified, the
      relative ordering of the two produces very different results.  In
      general, `ep' (evaluate then print) is what you want, but
      operating the other way round can be useful for debugging.  For
      this reason, the current version of GNU `sed' interprets specially
      the presence of `p' options both before and after `e', printing
      the pattern space before and after evaluation, while in general
      flags for the `s' command show their effect just once.  This
      behavior, although documented, might change in future versions.
 
 `w FILE-NAME'
      If the substitution was made, then write out the result to the
      named file.  As a GNU `sed' extension, two special values of
      FILE-NAME are supported: `/dev/stderr', which writes the result to
      the standard error, and `/dev/stdout', which writes to the standard
      output.(1)
 
 `e'
      This command allows one to pipe input from a shell command into
      pattern space.  If a substitution was made, the command that is
      found in pattern space is executed and pattern space is replaced
      with its output.  A trailing newline is suppressed; results are
      undefined if the command to be executed contains a NUL character.
      This is a GNU `sed' extension.
 
 `I'
 `i'
      The `I' modifier to regular-expression matching is a GNU extension
      which makes `sed' match REGEXP in a case-insensitive manner.
 
 `M'
 `m'
      The `M' modifier to regular-expression matching is a GNU `sed'
      extension which causes `^' and `$' to match respectively (in
      addition to the normal behavior) the empty string after a newline,
      and the empty string before a newline.  There are special character
      sequences (`\`' and `\'') which always match the beginning or the
      end of the buffer.  `M' stands for `multi-line'.
 
 
    ---------- Footnotes ----------
 
    (1) This is equivalent to `p' unless the `-i' option is being used.
 
Info Catalog (sed.info.gz) Common Commands (sed.info.gz) sed Programs (sed.info.gz) Other Commands
automatically generated byinfo2html