问题
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:
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
来源:https://stackoverflow.com/questions/49074364/sed-command-ignoring-single-quotes-in-groovy-scripts