Question

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.

Was it helpful?

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top