sed: The "s" Command

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