3

I am asking for help now because I have been struggling with a simple sed command to be called inside a Jenkinsfile that needs a little variable interpolation.

Better showing the command instead of a large explanation:

sh "sed -i -e 's/-RELEASE/-${unixEpoch}/g' myFile"

sed does not agree with this syntax and prints that the command s/ is not finished.

I have read Groovy documentation about String and GString but I still don't understand what I am doing wrong ?

Any clues on this?

EDIT:

I am getting the unixEpoch by calling date +%s in order to get the current timestamp.

I printed the command just to be sure what's executed and I found:

sed -i -e 's/-RELEASE/-1525341883'
/g' myFile

The full error sent by sed is:

sed: -e expression #1, char 22: unterminated 's' command

I found weird that the printed command has an \n in the middle of it...

MadJlzz
  • 767
  • 2
  • 13
  • 35
  • I'm unable to reproduce this on Ubuntu 16.04 with a `unixEpoch` value of `123456` (for testing). Can you share a bit more about how you are executing it? – mkobit May 03 '18 at 14:14
  • @mkobit I added some more context. I think I found the solution. It seems that the command `date` is adding a new line at the end of the string causing `sed` to explode... – MadJlzz May 03 '18 at 14:35

1 Answers1

2

date +%s has a newline at the end, and when you interpolate it into your generated sed it is including that newline which explains why sed is complaining. You could do ${unixEpoch.trim()} or trim the unixEpoch value before using it.

mkobit
  • 43,979
  • 12
  • 156
  • 150