Alex
Alex

Reputation: 2412

Why is this regex missing quotes?

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?

Upvotes: 1

Views: 99

Answers (1)

revo
revo

Reputation: 48751

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'

Upvotes: 1

Related Questions