special characters in sed
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.
mnr
Updated on June 25, 2022Comments
-
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 ingrep
are?-
Mat over 11 yearsThe documentation for sed (and grep) is what you're looking for. What didn't you find in there?
-
mnr over 11 yearsI just see examples of what are special characters, but I am never sure that it is the complete list of special characters
-
Mat over 11 yearsI'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 over 11 yearsAlso depends on the dialect. In Linux
sed
the characters()|+?
are not special by themselves, only if preceded by a backslash. -
tripleee over 11 yearsAlso inside a character class
^-]
(but no others) are special. It's a separate mini-language, really. -
Jonathan Leffler over 11 yearsInside 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 ofsed
elaborates on that. For any specific system, look at the local manual page.