英文:
How to replace a character having { special character using a perl command
问题
我正在使用以下Perl命令,但值未填充到我的HTML文件中:
使用的命令:
perl -l -p -i -e "s/BER_IMC_SRE_{10}/$BER_IMC_SRE_10/g" $tmpfile501
perl -l -p -i -e "s/BER_IMC_DB_Lat_SRE_{10}/$BER_IMC_DB_Lat_SRE_10/g" $tmpfile501
执行日志:
perl -l -p -i -e 's/BER_IMC_SRE_{10}/10228/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
perl -l -p -i -e 's/BER_IMC_DB_Lat_SRE_{10}/9/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
HTML输出:
我想要将变量BER_IMC_DB_Lat_SRE_{10}替换为执行日志中显示的值9。
-
perl -l -p -i -e 's/BER_IMC_SRE_{10}/10228/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
-
perl -l -p -i -e 's/BER_IMC_DB_Lat_SRE_{10}/9/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
英文:
I am using below perl command & but the value is not populating into my html file w.r.t. to it .
commands used :
perl -l -p -i -e "s/BER_IMC_SRE_{10}/$BER_IMC_SRE_10/g" $tmpfile501
perl -l -p -i -e "s/BER_IMC_DB_Lat_SRE_{10}/$BER_IMC_DB_Lat_SRE_10/g" $tmpfile501
execution logs :
perl -l -p -i -e 's/BER_IMC_SRE_{10}/10228/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
perl -l -p -i -e 's/BER_IMC_DB_Lat_SRE_{10}/9/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
HTML O/P :
I want to replace the variable BER_IMC_DB_Lat_SRE_{10} with value 9 as shown in the execution logs .
-
perl -l -p -i -e 's/BER_IMC_SRE_{10}/10228/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
-
perl -l -p -i -e 's/BER_IMC_DB_Lat_SRE_{10}/9/g' /data/scripts/reports/20230629125556374622858//24949_rep.html
答案1
得分: 1
花括号在正则表达式中是特殊字符。需要转义它们:
perl -l -p -i -e "s/BER_IMC_SRE_\{10\}/$BER_IMC_SRE_10/g"
您还可以使用\Q
来让quotemeta为您转义所有字符:
perl -l -p -i -e "s/\QBER_IMC_SRE_{10}/$BER_IMC_SRE_10/g"
请注意,如果替换字符串中包含/
,这种方法仍可能失败。最好使用Perl变量而不是shell变量:
r=$BER_IMC_SRE_10 perl -l -p -i -e 's/\QBER_IMC_SRE_{10}/$ENV{r}/g'
英文:
Curly brackets are special in regexes. Escape them:
perl -l -p -i -e "s/BER_IMC_SRE_\{10\}/$BER_IMC_SRE_10/g"
You can also use the \Q
to let quotemeta escape everything for you:
perl -l -p -i -e "s/\QBER_IMC_SRE_{10}/$BER_IMC_SRE_10/g"
Note that it can still fail if the replacement contains a /
. It's better to use Perl variables rather than shell variables.
r=$BER_IMC_SRE_10 perl -l -p -i -e 's/\QBER_IMC_SRE_{10}/$ENV{r}/g'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论