1

我有一个名为“silent.txt”的文件。该文件有如下一行

bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName

我想用上面的文字替换

bop4InstallDir = "/abc/xyz/pqr"

使用 groovy 脚本我该如何做到这一点?请帮忙。

4

3 回答 3

3

不是很优雅,但这应该可以。

def file = new File("silent.txt")

file.text = file.text.replace('bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName', 
'bop4InstallDir = "/abc/xyz/pqr"')
于 2014-02-10T06:39:46.807 回答
0

silent.txt 是格式良好的属性文件吗?在这种情况下,您可以使用多种方式来访问它们,比愚蠢的替换更安全。

看起来很时髦:如何访问属性文件?ConfigSlurper

于 2014-02-10T07:01:21.730 回答
0

以下代码有效:

def file = new File("silent.txt")
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"")
    file.write(fileText);
于 2014-02-13T09:34:48.073 回答