1

I'm trying to write a shell script to increment a build number of a version stored in a JSON file.

{
  /**
   * The application's namespace.
   */
  "name": "AppName",

  /**
   * The version of the application.
   */
  "version": "1.0.0.23",

  /**
   * More comments.....
   */
  ....
}

I've already tested a simple way to increment the number if the variable was already in the file:

version='1.0.0.23'
a=( ${version//./ } )                   # replace points, split into array
((a[3]++))                              # increment revision (or other part)
version="${a[0]}.${a[1]}.${a[2]}.${a[3]}"       # compose new version
echo $version                                   # outputs: 1.0.0.24

I've looked at jq and jshon but neither will parse the JSON file because it contains comments (the app.json file is generated automatically by Sencha Cmd)

How can I read the version property using something like awk / sed and update it? Would jsawk be better?

Update I've managed to extract the version number using this: version=$( sed -n 's/.*"version": "\(.*\)",/\1/p' app2.json ) so now I can read the version and increment it. Just need to write it back now.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Natalie J
  • 41
  • 1
  • 5
  • 2
    Isn't there an option to omit the comments? Can't believe that a tool produces `*.json` files which aren't valid json. – hek2mgl Apr 04 '16 at 12:11
  • seems to be the Sencha standard: https://docs.sencha.com/cmd/6.x/microloader.html – Natalie J Apr 04 '16 at 12:26
  • Hmm, I've no idea about sencha. I've added the tag sencha. It's more likely that you'll find somebody who knows then. – hek2mgl Apr 04 '16 at 12:48
  • This question has been answered before in detail here: http://stackoverflow.com/a/36340305/4775223. This answer has two parts – Wilmer SH Apr 04 '16 at 13:41

3 Answers3

3

With awk:

awk -F'["]' -v OFS='"'  '/"version":/{
    split($4,a,".");
    $4=a[1]"."a[2]"."a[3]"."a[4]+1
    }
;1' app.json

with IFS and OFS as " find line that matches "version":.

split($4,a,".") : split the fourth field with . as separator and save to array a.

$4=a[1]"."a[2]"."a[3]"."a[4]+1 : Reassign the $4 with new value.

1 : print all the lines

If input line is "version": "1.0.0.23",

O/P will be "version": "1.0.0.24",

jijinp
  • 2,592
  • 1
  • 13
  • 15
  • Specify file at the end of awk command. Avoid [Useless use of cat](https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat). – jijinp Apr 04 '16 at 13:41
3

I continued experimenting with sed and managed to produce this:

# get the existing version number
ver=$( sed -n 's/.*"version": "\(.*\)",/\1/p' app2.json )    # ver='1.0.0.43'
echo "Current version: $ver"

a=( ${ver//./ } )                           # replace points, split into array
((a[3]++))                                 # increment revision (or other part)
newVer="${a[0]}.${a[1]}.${a[2]}.${a[3]}"   # compose new version
echo "New version: $newVer"                 # newVer='1.0.0.44'

# write output
sed -i .temp "s/\"version\": \"$ver\"/\"version\": \"$newVer\"/g" app2.json

which does the job.

Natalie J
  • 41
  • 1
  • 5
  • I used parameter expansion `v=$(cat package.json | grep -m 1 version | sed 's/[^0-9.]//g')` // v = 1.10.210 `p=${v##*.}` // p = 210 `uP=$((p+1))` // uP = 211 `sed -i '' 's/\"version\": \"'$v'\"/\"version\": \"'$mV'\"/g' package*.json; git checkout -b hotfix/$mV;` // change both package.json and package-lock.json. and I used this in git alias so double quotes and quotes are confusingly mixed – Bo Hyeon Seo Mar 03 '22 at 11:50
3

Building off of 7171u's answer, here is a complete one-liner that overwrites the original json file with the patch incremented version:

awk -F'["]' -v OFS='"'  '/"version":/{split($4,a,".");$4=a[1]"."a[2]"."a[3]+1};1' ./manifest.json > ./manifest2.json && mv ./manifest2.json ./manifest.json

This will get the current version from the json file, increment the patch version position, write it out to a new file, then in the second statement, replace the original file with the new one.

Chad Michael
  • 266
  • 2
  • 8