-1

我正在尝试替换文件中的字符串,但我的程序似乎正在删除字符串而不是用我想要的替换它

例子

perl -ne 'print unless s/ServerIp=${Solid.host}/ads/;' needsToBeReplaced.prp > blah.txt

将删除

"ServerIp=${Solid.host}" 并将该行留空,而不是在其位置打印“广告”

顺便说一句,我在 Windows 中运行。另外我在正则表达式中没有第三个参数,因为我只想更改第一次出现。我也试过

perl -pi -e 's/ServerPort=${Solid.port}/ads/;' needsToBeReplaced.txt 

但是我得到了权限错误,所以就地编辑是不行的

4

2 回答 2

6

You print the line only if the substitution is not successful. If you want to print always, do not use unless:

perl -pe "s/ServerIp=\${Solid.host}/ads/;" needsToBeReplaced.prp > blah.txt

You should also escape the dollar sign to prevent its interpretation by Perl.

Also note that double quotes must be used on MS Windows.

于 2013-04-17T22:46:21.723 回答
0

With what you've got now, if the substitute succeeds, it will return a true value, which means unless will be true, which means nothing will be printed. You could do it like this:

perl -ne 's/ServerIp=${Solid.host}/ads/; print;' needsToBeReplaced.prp > blah.txt
于 2013-04-17T22:48:54.600 回答