1
votes

Hello I am facing this issue when I try to replace string using SED in Groovy, it's ignoring the Single quotes which I am passing. Here is my code I tried using double quotes inside sed and it's throwing errors.

            stage('Version')
        {
            dir('./Dest/Scripts/')
            {
                    sh "sed -i 's/VERSION_BUILD=0/VERSION_BUILD= '$Version', System2 = '$name'/g' setversion.sql"              
            }
        }

My desired output is

UPDATE &Shared_Version SET SharedVersion = '2010', System1 = 'XXXX', InetsoftVersion = 2, VERSION_BUILD='20180302', System2 = 'test';
COMMIT;

however I am getting below results after I run the Groovy script.

UPDATE &Shared_Version SET SharedVersion = '2010', System1 = 'XXXX', InetsoftVersion = 2, VERSION_BUILD= 20180302, System2 = test;
COMMIT;

I do know in shell Command if we pass double quotes it will replace, however Groovy is not liking it.

sed -i "s/VERSION_BUILD=0/VERSION_BUILD= '$Version', System2 = '$name'/g" setversion.sql 

Can some one help me to address this problem here.

Thanks

1
What about: sh $/sed -i "s/VERSION_BUILD=0/VERSION_BUILD= '$Version', System2 = '$name'/g" setversion.sql/$ - tim_yates
@tim_yates I am getting below error. groovy.lang.MissingPropertyException: No such property: $ for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:242) - saint
Stumped I'm afraid. Hopefully someone with Jenkins knowledge can jump in - tim_yates
have you tried triple quote version? - Jayan

1 Answers

1
votes

Following script works as you expected

sh "sed -i 's/VERSION_BUILD=0/VERSION_BUILD= \\x27${Version}\\x27, System2 = \\x27$name\\x27/g' setversion.sql"

The point is that single quote can be escaped as \x27