Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

special characters in sed

Tags:

regex

grep

sed

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?

like image 376
mnr Avatar asked Sep 16 '12 11:09

mnr


People also ask

What are the special characters in sed?

The special character in sed are the same as those in grep, with one key difference: the forward slash / is a special character in sed. The reason for this will become very clear when studying sed commands.

How do I use special characters in sed command?

Special characters, preceded by \ 's will be taken literally. (Like you've done with your parentheses: ( \( \) )).

How do you escape special characters in sed?

Put a backslash before $. */[\]^ and only those characters (but not inside bracket expressions).


2 Answers

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.

like image 198
William Pursell Avatar answered Sep 28 '22 02:09

William Pursell


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

like image 20
mnr Avatar answered Sep 28 '22 03:09

mnr