Search code examples
jsonsed

Bump version with sed in package.json


I need to make an automatic bump of package.json in jenkins, and really stuck with it.

The following sed command works in a pipeline but it changes the second field, I need to change the third one:

sed -i  "/version/s/\\([.]\\)\\(.\\)\\(.*\\)/\\1${BUILD_NUMBER}\\3/" package.json

Output:

"version": "0.222.0"

but I need:

"version": "0.0.222"

a part of json input:

{
 "name": "render",
  "version": "0.0.0"
  "description": "",
 "main": "./dist/index.js",
   "bin": {
"render-ne": "./bin/re"
  },

Who can assist with it?


Solution

  • Assuming the field format is as was presented, (i.e.: "version": "0.0.0"), then those back references aren't really necessary here. Just substitute the text after the final .:

    sed  -i '/version/s/[^.]*$/'"${BUILD_NUMBER}\"/" package.json