-4

The command I am using is

sed -i "s/01.00.*/${version}/g" file.json

Result: "${version}

Expected "${version}"

Example:

"params": {
        "pipeline_branch":"master",
        "git_repo_url":"blah.git",
        "ARTIFACTORY_TAR_VERSION" : "01.00.00.26"
}

I want to be able to sed and replace 01.00.00.26 value ( or any 01.00.* which keeps changing, typically pom version ) with the value of "${version}" which is being retrieved passed through the shell.

I'm intending to run this on Linux through a Jenkins job. The sed command I use works (almost) perfectly, but removed the double quote in the last.

  • 4
    The only difference between your "Result" and the "Expected" is one quote? Do you want to use the literal string ${version}, or whatever the value of the shell variable version is? And with quotes or not...? – ilkkachu Jul 24 '18 at 21:17
  • @ilkkachu Yes, you are right. Since I use * it truncates the quote " too. I would need a value of ${version} which would be some thing similar to 01.00.23.26 – wizzy Jul 25 '18 at 13:20
  • @roaima updated – wizzy Jul 25 '18 at 14:10
0

The problem with your sed command is that it matches too much. Let's look at the pattern, then break it down:

/01.00.*/
 01       # exact match (so far, so good)
   .      # any character (too generous)
    00    # exact match
      .*  # the rest of the line

What we want to match is 01.00. followed by any number of digits and dots:

/01\.00\.[.0-9]*/

Note that we use \. to match the dots, instead of . which matches any single character.

-1

The relevant line is this one

"ARTIFACTORY_TAR_VERSION" : "01.00.00.26"

There are a couple of options: you could require "ARTIFACTORY_TAR_VERSION" to appear on the line, and then substitute something that looks like a version number, or you could be less cautious and just substitute anything that looks like a version number.

This option will replace anything that looks like a version number that contains 01.00. and is followed by digits and dots:

sed -i "s/01\.00\.[0-9.]*/${version}/g" file.json

This one constrains the pattern to be bounded by double quotes (notice I switch mid-way through the RE from single-quoted to double-quoted protection against the shell interpreting the pattern):

sed -ri 's/(")[[:digit:].]*"/'"\1${version}\1/g" file.json

This final one also requires the pattern to match only on lines containing the word VERSION somewhere in them:

sed -ri '/VERSION/s/(")[[:digit:].]*"/'"\1${version}\1/g" file.json

Don't forget that for testing you don't necessarily need to modify the file in situ. You can omit the -i flag and inspect the output without changing the source file.

  • tried the first option which is sed -i "s/01\.00\.[0-9.]*/${version}/g" file.json And i get the error : unexpected char: '\' – wizzy Jul 25 '18 at 19:37
  • @wizzy what OS are you running this on? – roaima Jul 25 '18 at 20:01
  • Also the option 2 which is sed -i sed -ri 's/(")[[:digit:].]*"/01.00.00.246/g' doesn't work :( – wizzy Jul 25 '18 at 20:12
  • Running it on Linux through a Jenkins job. The sed command i use works perfectly but removed the double quote in the last. sed -i "s/01.00.*/${version}/g" file.json Can i do one more sed to add a double quote in the last which is all i need – wizzy Jul 25 '18 at 20:14
  • @wizzy they work perfectly for me at a (bash) shell prompt. Perhaps your Jenkins environment does something strange with backslashes? (I'm not familiar with Jenkins.) – roaima Jul 25 '18 at 22:01
-2

sed -i "s/01.00.*\"/\${version}\"/g" file.json

Search the line till -- s/01.00.*\"/ Replace with -- /${version}\"/

  • Why do you add the i there? – ilkkachu Jul 25 '18 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.