在 Windows 上使用包含美元符号的字符串进行 Perl 替换 [英] Perl substitution using string that contains a dollar sign on windows

查看:51
本文介绍了在 Windows 上使用包含美元符号的字符串进行 Perl 替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上使用 perl,并尝试使用 perl 做一个单行,以使用包含美元符号的 windows 变量替换文件中的占位符.有谁知道使用美元符号的正确用法是什么.我尝试了各种方法,但似乎无法让它发挥作用.

I'm using perl on windows and am trying to do a one liner using perl to substitute a placeholder in a file using a windows variable that contains a dollar sign. Does anyone know what the correct usage is to make it work with the dollar sign. I've tried various ways and can't seem to get it to work.

例如,我有一个属性文件,其中包含一个令牌 (!MYPASSWORD!),我试图将其替换为:

For example, I have a properties file that has a token in it (!MYPASSWORD!) that I'm trying to replace like:

somevalue="!MYPASSWORD!"

我有一个批处理文件,它查找一个名为 NEWPASSWORD 的变量,其中包含密码 $abc12345$,我想使用 perl 替换来替换如下所示的值.注意我可能并不总是知道 $ 符号在哪里,所以我无法逃避它们.例如另一个密码可能是 abc$124$563:

I have a batch file that looks up a variable say called NEWPASSWORD that contains the password $abc12345$ and I want to use perl substitution to replace the value like the following. Note I may not always know where the $ signs are so I cant escape them. For example another password may be abc$124$563:

echo %NEWPASSWORD%   <-- this would contain $abc12345$
perl -p -i.bak -e "s/!MYPASSWORD!/%NEWPASSWORD%/g" a.properties

完成后我希望 a.properties 为:

When its done I want a.properties to be :

somevalue="$abc12345$"

提前致谢

推荐答案

使用 ' 作为正则表达式分隔符.它将禁用所有变量替换:

Use ' as regexp delimeter symbol. It will disable all variable substitution:

perl -p -i.bak -e "s'!MYPASSWORD!'%NEWPASSWORD%'g" a.properties

这篇关于在 Windows 上使用包含美元符号的字符串进行 Perl 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆