문제

I have got a file with name "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? Please help.

도움이 되었습니까?

해결책 3

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);

다른 팁

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"')

Is silent.txt well formatted property file? 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top