[英]Replace the string in file using groovy
I have got a file with name "silent.txt". 我有一个名为“ silent.txt”的文件。 This file is having a line as follows
该文件的一行如下
bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName
I want to replace the above text with 我想将上面的文本替换为
bop4InstallDir = "/abc/xyz/pqr"
Using groovy script how do I accomplish this? 使用groovy脚本我该如何完成? Please help.
请帮忙。
Not very elegant, but this should work. 不是很优雅,但这应该可行。
def file = new File("silent.txt")
file.text = file.text.replace('bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName',
'bop4InstallDir = "/abc/xyz/pqr"')
The following code worked: 以下代码有效:
def file = new File("silent.txt")
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"")
file.write(fileText);
Is silent.txt well formatted property file? Silent.txt格式正确的属性文件吗? In this case you can use a various ways to access them, much more secure, than dumb replace.
在这种情况下,您可以使用多种方法来访问它们,这些方法比伪装替换要安全得多。
Look groovy: How to access to properties file? 看起来很时髦:如何访问属性文件? or ConfigSlurper
或ConfigSlurper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.