【发布时间】:2021-07-11 05:24:39
【问题描述】:
我想用 Perl 替换包含 @ 字符的子字符串,如下面的 sed 命令:
substitution='newusername@anotherwebsite.com'
sed 's/oldusername@website.com/'"${substitution}"'/g' <<< "The current e-mail address is oldusername@website.com"
目前,无论我使用 Perl 而不是 sed 或 awk,我都会先将 \ 替换为 \\,将 / 替换为 \/,将 $ 替换为 \$,将 @ 替换为 \@;例如
substitution='newusername@anotherwebsite.com'
substitution="${substitution//\\/\\\\}"
substitution="${substitution//\//\\/}"
substitution="${substitution//$/\\$}"
substitution="${substitution//@/\\@}"
perl -pe 's/oldusername\@website.com/'"${substitution}"'/g' <<< "The current e-mail address is oldusername@website.com"
我已阅读有关使用单引号的信息(如下基于sed/ perl with special characters (@)),但我想知道是否有任何其他方法可以使用正斜杠来做到这一点?
substitution='newusername@anotherwebsite.com'
perl -pe "s'oldusername@website.com'"${substitution}"'g" <<< "The current e-mail address is oldusername@website.com"
另外,Perl 中除了$、@ 和% 之外还有特殊字符(以及为什么不需要转义%)?
【问题讨论】: