1

I'm trying to use sed to increment a version number in a conf file. The version number is of this form: MENDER_ARTIFACT_NAME = "release-6".

Using the following:

sed -r 's/(.*)(release\-)([0-9]*)(.*)/echo "\1\2$((\3+1))\4"/ge'

The result, is this: MENDER_ARTIFACT_NAME = release-7

I.E. it works, but it misses the quotes. I've checked the regex docs, and (.*) should match all non newline characters, any number of times, so the first should match everything, including the quote, before release-6, and the second should match everything, including the quote, after release-6. Instead, it seems to drop the quotes completely. What am I doing wrong?

Alex
  • 2,270
  • 3
  • 33
  • 65
  • When you use solutions other people created, the nice thing to do is to refer to them. In this case, you took code from this answer: https://stackoverflow.com/a/14348899. – Tomalak Nov 11 '18 at 17:38
  • 1
    I did, yes. I didn’t realise that single lines of bash commands required attribution. Sorry. – Alex Nov 11 '18 at 17:39
  • @revo that makes sense, thanks. If you want to add it as an answer, I’ll accept it – Alex Nov 11 '18 at 17:47
  • I don't know about "require attribution", that's not even my point. I said *"the nice thing to do"*. As in, "I din't come up with this, I took it from here." It should not be legally required to do what's decent. – Tomalak Nov 11 '18 at 17:53
  • I didn't realise that such things were required for 'decency'. I've never expected to have single lines of my code credited, but if others do, then I shall do so in future. Consider me suitably contrite. – Alex Nov 11 '18 at 18:01
  • Not related to the question: *`release\-`* - outside of a character class, the dash (`-`) is just a regular character, there is no need to escape it. – axiac Nov 11 '18 at 19:22
  • @axiac Good point, thank you! – Alex Nov 11 '18 at 19:23

1 Answers1

1

As per documentation:

the e flag executes the substitution result as a shell command...

which means quotation marks are there for showing a bunch of characters. I.e try echo MENDER_ARTIFACT_NAME = "release-6". You should add escaped quotation marks in echo statement manually:

sed -r 's/^(.*)(release\-)([0-9]+)/echo "\1\\"\2$((\3+1))\\""/ge'
revo
  • 47,783
  • 14
  • 74
  • 117