[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sed what chars need \escape?

From: Nora Platiel
Subject: Re: sed what chars need \escape?
Date: Sun, 22 Nov 2020 21:49:27 +0100

> 1) Skip this step if you already have a sed regexp. If you have a string to be
> matched literally, you have to quote the following characters with a 
> backslash:
> $*.[\^
> If you are using extended regular expressions you also need to quote these:
> ()+?{|}

Oops, actually, as with ], you also don't need to quote }, even though you can.

> 2) The replacement part of the s-command is not a regexp, so 1) doesn't apply
> to it. In the replacement part you have to quote only \ and & which stand for
> portions of the match.

Ok, it seems that in a sed script, \C (unquoted backslash followed by any char) 
is either part of a special sequence, or it stands for a literal C. It is never 
invalid nor it can stand for a literal \ followed by a literal C.
Therefore you can probably have your escape tool produce strings that are good 
for both regexps and replacements (in that case you also have to quote &). Just 
be careful not to quote ', <, >, or ` in regexp, because it would produce 
special sequences.

> 4) Finally, if you want to embed your sed script into shell code you have to
> make sure it's treated literally by the shell.
> One way is to put it between single quotes after replacing any single quote 
> (')
> with \x27 (which is decoded by sed).

But note that if you use \' in a regexp to match the end of pattern space, you 
cannot replace that single quote with \x27 because it would lose its special 
meaning. In such case you have to use a different quoting style.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]