sed: Escapes

1 
1 5.8 Escape Sequences - specifying special characters
1 ====================================================
1 
1 Until this chapter, we have only encountered escapes of the form '\^',
1 which tell 'sed' not to interpret the circumflex as a special character,
1 but rather to take it literally.  For example, '\*' matches a single
1 asterisk rather than zero or more backslashes.
1 
1    This chapter introduces another kind of escape(1)--that is, escapes
1 that are applied to a character or sequence of characters that
1 ordinarily are taken literally, and that 'sed' replaces with a special
1 character.  This provides a way of encoding non-printable characters in
1 patterns in a visible manner.  There is no restriction on the appearance
1 of non-printing characters in a 'sed' script but when a script is being
1 prepared in the shell or by text editing, it is usually easier to use
1 one of the following escape sequences than the binary character it
1 represents:
1 
1    The list of these escapes is:
1 
1 '\a'
1      Produces or matches a BEL character, that is an "alert" (ASCII 7).
1 
1 '\f'
1      Produces or matches a form feed (ASCII 12).
1 
1 '\n'
1      Produces or matches a newline (ASCII 10).
1 
1 '\r'
1      Produces or matches a carriage return (ASCII 13).
1 
1 '\t'
1      Produces or matches a horizontal tab (ASCII 9).
1 
1 '\v'
1      Produces or matches a so called "vertical tab" (ASCII 11).
1 
1 '\cX'
1      Produces or matches 'CONTROL-X', where X is any character.  The
1      precise effect of '\cX' is as follows: if X is a lower case letter,
1      it is converted to upper case.  Then bit 6 of the character (hex
1      40) is inverted.  Thus '\cz' becomes hex 1A, but '\c{' becomes hex
1      3B, while '\c;' becomes hex 7B.
1 
1 '\dXXX'
1      Produces or matches a character whose decimal ASCII value is XXX.
1 
1 '\oXXX'
1      Produces or matches a character whose octal ASCII value is XXX.
1 
1 '\xXX'
1      Produces or matches a character whose hexadecimal ASCII value is
1      XX.
1 
1    '\b' (backspace) was omitted because of the conflict with the
1 existing "word boundary" meaning.
1 
1 5.8.1 Escaping Precedence
1 -------------------------
1 
1 GNU 'sed' processes escape sequences _before_ passing the text onto the
1 regular-expression matching of the 's///' command and Address matching.
1 Thus the follwing two commands are equivalent ('0x5e' is the hexadecimal
1 ASCII value of the character '^'):
1 
1      $ echo 'a^c' | sed 's/^/b/'
1      ba^c
1 
1      $ echo 'a^c' | sed 's/\x5e/b/'
1      ba^c
1 
1    As are the following ('0x5b','0x5d' are the hexadecimal ASCII values
1 of '[',']', respectively):
1 
1      $ echo abc | sed 's/[a]/x/'
1      Xbc
1      $ echo abc | sed 's/\x5ba\x5d/x/'
1      Xbc
1 
1    However it is recommended to avoid such special characters due to
1 unexpected edge-cases.  For example, the following are not equivalent:
1 
1      $ echo 'a^c' | sed 's/\^/b/'
1      abc
1 
1      $ echo 'a^c' | sed 's/\\\x5e/b/'
1      a^c
1 
1    ---------- Footnotes ----------
1 
1    (1) All the escapes introduced here are GNU extensions, with the
1 exception of '\n'.  In basic regular expression mode, setting
1 'POSIXLY_CORRECT' disables them inside bracket expressions.
1