2014-02-10 3 views
1

"silent.txt"라는 파일이 있습니다. 나는이를 어떻게 그루비 스크립트를 사용groovy를 사용하여 파일에서 문자열 바꾸기

bop4InstallDir = "/abc/xyz/pqr" 

와 위의 텍스트를 대체 할

bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName 

을 다음과 같이이 파일은 라인을 가지고있다? 도와주세요.

답변

1

다음 코드는 작동 :

def file = new File("silent.txt") 
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"") 
    file.write(fileText); 
1

별로 좋지는 않지만 제대로 작동합니다.

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

file.text = file.text.replace('bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName', 
'bop4InstallDir = "/abc/xyz/pqr"') 
+0

그것이 작동하지 않습니다. 오류는 "EOF를 찾았습니다. ')'@ 6 행 34 행입니다. 'bop4InstallDir ="/ abc/xyz/pqr "')" – user3291679

+0

따옴표를 잘못 입력했다고 생각합니다. 위의 코드를 그대로 복사 한 다음 시도하십시오. – kunal

+0

복사 된 코드가 작동하지 않았습니다. 나는 '/', '+'와 같은 특수 문자와 관련이 있습니다 ... – user3291679

관련 문제