1

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.

Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113
user3291679
  • 19
  • 1
  • 5

3 Answers3

3

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"')
kdabir
  • 9,623
  • 3
  • 43
  • 45
0

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

Community
  • 1
  • 1
Seagull
  • 13,484
  • 2
  • 33
  • 45
0

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);
Community
  • 1
  • 1
user3291679
  • 19
  • 1
  • 5