sed-devel
[Top][All Lists]
Advanced

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

s-command with \ (or other meta-chars) as delimiter when these are also


From: Christoph Anton Mitterer
Subject: s-command with \ (or other meta-chars) as delimiter when these are also used in the pattern or replacement?
Date: Fri, 07 Jan 2022 07:28:54 +0100
User-agent: Evolution 3.42.2-1

Hey.

Not that I'd have any real need for using it, but in the info manual of
sed I saw:

   The '/' characters may be uniformly replaced by any other single
character within any given 's' command.  The '/' character (or whatever
other character is used in its stead) can appear in the REGEXP or
REPLACEMENT only if it is preceded by a '\' character.


And out of curiosity... "any other single character"... does that also
mean those with special meaning?



$ printf '\\\n' | sed 's\[\]\-\'
-
=> works by using the bracket expression

$ printf '\\\n' | sed 'sX\\X-X'
-
=> works

E.g. following the above advise I should be able to use \ instead of X
when I also quote every occurrence \ with another \ so : 

$ printf '\\\n' | sed 's\\\\\\-\'
sed: -e expression #1, char 5: unknown option to `s'
=> nope...


Similarly:
$ printf '[\n' | sed 'sX\[X-X'
-
=> works

but none of:
$ printf '[\n' | sed 's[\\[[-['
sed: -e expression #1, char 7: unknown option to `s'
$ printf '[\n' | sed 's[\\\[[-['
sed: -e expression #1, char 9: Invalid regular expression



Interestingly it can be made working or .:

$ printf '%s\n' '-.-' | sed 'sX\.X#Xg'
-#-
=> works

$ printf '%s\n' '-.-' | sed 's.\..#.g'
###
=> \. quote just does away the delimiter meaning, but keeps the special
meaning of .

$ printf '%s\n' '-.-' | sed 's.\\..#.g'
-.-
$ printf '%s\n' '-.-' | sed 's.\\\..#.g'
-.-
$ printf '%s\n' '-.-' | sed 's.\\\\..#.g'
-.-
$ printf '%s\n' '-.-' | sed 's.\\\\\..#.g'
-.-

=> these are weird... shouldn't at least some of them give an error at
least (i.e. when even number of \ )?



So in general, is it possible to use these special characters as
delimiters or only if these characters themselves are not used
literally or with their special meaning in the pattern or the
replacement?
Are there any rules how to do it?


Thanks,
Chris.



reply via email to

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