special characters in sed

17,407

Solution 1

It depends. Strictly speaking, a standard compliant sed must only use Basic Regular Expressions for which the standard states:

The BRE special characters and the contexts in which they have their special meaning are as follows:

.[\ The period, left-square-bracket, and backslash shall be special except when used in a bracket expression (see RE Bracket Expression ). An expression containing a '[' that is not preceded by a backslash and is not part of a bracket expression produces undefined results.

* The asterisk shall be special except when used in a bracket expression, as the first character of an entire BRE (after an initial '^' , if any), or as the first character of a subexpression (after an initial '^' , if any); see BREs Matching Multiple Characters

^ The circumflex shall be special when used as an anchor (see BRE Expression Anchoring ) or as the first character of a bracket expression (see RE Bracket Expression )

$ The dollar-sign shall be special when used as an anchor.

So the complete list is .[\*^$, but context matters. Also, many sed provide options to use extended regular expressions(EREs), which will expand the list and change the context in which characters are special. For example, without EREs groupings are formed using \( and \), but with EREs ( and ) by themselves are special and must be escaped to be matched literally.

Solution 2

I think this is the full list of characters [\^$.|?*+() on which sed will respond in a manner different than a normal character.

Share:
17,407
mnr
Author by

mnr

Updated on June 25, 2022

Comments

  • mnr
    mnr almost 2 years

    Does anybody know what the complete list of special characters in sed are ?

    Please don't give an answer like, it is the same list of special characters for grep, because that just transforms my question to: Does anybody know what the complete list of special characters in grep are?

    • Mat
      Mat over 11 years
      The documentation for sed (and grep) is what you're looking for. What didn't you find in there?
    • mnr
      mnr over 11 years
      I just see examples of what are special characters, but I am never sure that it is the complete list of special characters
    • Mat
      Mat over 11 years
      I'm not sure even what you mean by "special character", but the documentation lists everything you can do. There's no point in replicating that documentation here.
  • tripleee
    tripleee over 11 years
    Also depends on the dialect. In Linux sed the characters ()|+? are not special by themselves, only if preceded by a backslash.
  • tripleee
    tripleee over 11 years
    Also inside a character class ^-] (but no others) are special. It's a separate mini-language, really.
  • Jonathan Leffler
    Jonathan Leffler over 11 years
    Inside a character class under POSIX, you can also write locale-sensitive expressions such as [[:alpha:]] or [[:punct:][:digit:]], so : also has a special meaning — and it is (yet another) mini-language. The URL lists the behaviour of POSIX-standard BRE (basic regular expression) and ERE (extended regular expression). The POSIX description of sed elaborates on that. For any specific system, look at the local manual page.