orange orange - 5 days ago 6
Groovy Question

jenkins escape sed command

Can someone escape this sed shell command in Jenkins groovy script for me?

So hard.

sh ("""
sed "s/(AssemblyInformationalVersion\(\")(.*)(\")/\1${productVersion}\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")

Answer

The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape". Since the first open paren is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash.

Thus:

sh ("""
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g" 
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")
Comments