How to properly escape a backreference in sed to pass it to a function?
The presented code properly handles the backreference. The backreference inside command substitution is not and will not be parsed by sed
, but by shell, before running sed
. The arguments to a program have to be expanded before running the program.
You may potentially use a GNU extension to sed
- the e
flag to s
command that executes the replacement pattern via /bin/sh
interpret. Using this flag is highly discouraged and is very hard to use, as figuring the correct quoting and escaping is very hard - it "works" in very simple cases. Because the input string has ;
<
>
and also "
special shell characters I doubt it's possible.
I suggest to pick a full fledged programming language, like python, perl or others, to solve your task. sed
is not an utility for dynamically executing actions depending on contents of the file, it's a simple stream replacement utility.
In sed
, it is possible to build a static list of strings to replace, like so:
sed -r '
s/(<CHARSET c="T">)02C8/1'"$(echo -e "u02C8")"/
s/(<CHARSET c="T">)0252/1'"$(echo -e "u0252")"/
.... one s/// command for each character to replace ...
'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…