Reputation: 1621
I'm doing a simple substitution, and this works fine at the command line:
sed "s/pub Url =.*/pub Url = 'https:\/\/example.com:3207';/g" myfile.ts
I'm trying to run it within a Jenkinsfile, and like 40 builds later I cannot get the escape quoting right.
Pretty sure it will look something like this:
sh 'sed \\"s/pub Url =.*/pub Url = \\'https:\\\/\\\/example.com:3207\\';/g\\" myfile.ts'
Yet that results in the following error:
WorkflowScript: 4: unexpected char: '\' @ line 4, column 49.
ub Url =.*/pub Url = \\'https:\\\/\\\/ex
I feel like I've tried dozens of variants but nothing is working.
Here is among the most common errors I'm getting: which points to escaping issue
sed: -e expression #1, char 1: unknown command: `"'
I really just need a pipeline expert that can likely see exactly what I'm doing wrong and know where to quote it.
As noted here: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4 this is not uncommon to fight this type of stuff in the pipeline files and it seems like it's just trial and error.
Upvotes: 2
Views: 7135
Reputation: 189387
Absurdly, through trial and error, I learned that backslashes need to be doubled even inside triple single quotes in a Jenkinsfile
. So the mechanical solution to your problem would look like
sh '''sed "s/pub Url =.*/pub Url = 'https:\\/\\/example.com:3207';/g" myfile.ts'''
However, you can avoid the backslashitis by using a different delimiter. Just make sure you choose one which doesn't occur in the text you need to process.
sh '''sed "s%pub Url =.*%pub Url = 'https://example.com:3207';%g" myfile.ts'''
A more complex scenario is when you try to use backreferences in sed
. If you naively try
sh '''sed 's/foo\(bar\|baz\)/quux\1/' file'''
you get
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 1: unexpected char: '\' @ line 1, column 17.
sh '''sed 's/foo\(bar\|baz\)/quux\1/' file'''
^
Wrong as it looks, the solution is to double those backslashes.
sh '''sed 's/foo\\(bar\\|baz\\)/quux\\1/' file'''
Upvotes: 0
Reputation: 61
To add more on this... I had an issue with sed -i 's/\_/\//g' abc.txt
command running fine in CLI but resulting in an unxpected char \
error in jenkins, so, i had to replace this:
sed -i 's/\_/\//g' abc.txt
with
sed -i "s/\\_/\\//g" abc.txt
To remove this error from jenkins.
Upvotes: -1
Reputation: 1621
Ok after much trial and error, this is working.
Looks like I had to use triple single quotes around the command. Good thing I don't need to interpolate!
sh '''sed \"s/pub Url =.*/pub Url = \\'https:\\/\\/example.com:3207\\';/g\" afile.txt'''
Hope this is helpful to someone in the future that's fighting this!
Upvotes: 3