How to increment a numerical value in properties file

sed increment number
shell script increment counter in a loop

I have a version.properties file as below:

#some comments
major=2
minor=16
patch=4

I want to increase the minor value by 1. so new value should be 16 +1 = 17. So the updated file will be:

#some comments
major=2
minor=17
patch=4

Is it possible to achieve this through bash script?

Thanks in advance

You could use awk:

awk -F"=" 'BEGIN{OFS=FS} $1=="minor"{$2=$2+1}1' versions.properties

This awk script says:

  1. Split each record by a an equal sign (-F"=")
  2. Ensure that the output field separator OFS is equal to the incoming field separator FS (BEGIN{OFS=FS})
  3. If the first field is the word "minor" ($1=="minor")
  4. Then add 1 to the second field ({$2=$2+1})
  5. Finally print the record back out 1.

You can redirect this to a temp file and then mv the temp back over the top of the original:

 awk -F"=" 'BEGIN{OFS=FS} $1=="minor"{$2=$2+1}1' versions.properties > versions.properties.tmp && mv versions.properties.tmp versions.properties

Can Ant increment a build number?, By auto-incrementing a number from a properties file, and then /> <replace file="${build-dir}/version.html" value="value not found in  You can set this property in two different ways: 1. Design-Time: It is the easiest way to set the increment value in the NumericUpDown as shown in the following steps: Step 1: Create a windows form as shown in the below image: Visual Studio -> File -> New -> Project -> WindowsFormApp

You can also use this:

#! /bin/bash
LINE=$(cat version.properties | grep minor)
VAL=$(echo $LINE | cut -f 2 -d "=")
VAL=$(($VAL + 1))
NEW_LINE=$(echo minor=$VAL)
sed -e "s,$LINE,$NEW_LINE,g" -i version.properties

It fixes the issue without the need to use additional redirections and mv commands. Hope this helps :-)

Increment a numerical string, Trim(Source => Integer'Image(Integer'Value(S) + 1), Side => Ada. property |λ| : f The following example also increments a numerical string, although it does not apear to. num-str is implicitly defined as USAGE DISPLAY which means its  Increment a numerical string You are encouraged to solve this task according to the task description, using any language you may know.

Steps:

1- Get line number of your parameter (to use in replacement)

grep -n "minor" version.properties | cut -d: -f1

This shows "3" according to your version.properties file

We save it in a variable

line=$(grep -n "minor" version.properties | cut -d: -f1)

2- Get key=value

grep -n "minor" version.properties | cut -d: -f2

This shows minor=16 according to your version.properties file

We save it in a variable called string:

string=$(grep -n "minor" version.properties | cut -d: -f2)

3- Get version of this string : minor=16

version=$(echo $string | cut -d= -f2)

4- Increase version

newVersion=$(( version + 1))

5- Replace in original properties file using line number:

sed -i "$line c $key=$newVersion" version.properties

Finally this is a complete script , with key and file as parameters, so you could use it to increase any key like in any properties file, in any location :

key=$1
file=$2

line=$(grep -n "$key" $file | cut -d: -f1)
string=$(grep -n "$key" $file | cut -d: -f2)

# get version from string
version=$(echo $string | cut -d= -f2)

#increase version
newVersion=$(( version + 1))

#update file
sed -i "$line c $key=$newVersion" $file

Usage:

bash update_version.sh "patch" /server/files/version.properties

Note : keys in file must be unique.

BuildNumber Task, It will first attempt to read a build number from a file (by default, build.number in the set the property build.number to the value that was read in (or to 0 , if no such value). It will then increment the number by one and write it back out to the file. The following are code examples for showing how to use kivy.properties.NumericProperty().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Article: How to increment a dynamic process property counter , Use a Set Properties shape to initialize a Dynamic Process Property after the start shape. Use a Data Process shape with a Custom Scripting step to get, increment, and set the property value. For an example of this in action including sample script, see How to Create a Do-While Number of Views 10.55K. Controls.Add (numericUpDown1) End Sub ' Check box to toggle decimal places to be displayed. Private Sub checkBox1_Click (sender As Object, e As EventArgs) ' If DecimalPlaces is greater than 0, set them to 0 and round the ' current Value; otherwise, set DecimalPlaces to 2 and change the ' Increment to 0.25.

Properties (The Java™ Tutorials > Essential Classes > The Platform , Properties are configuration values managed as key/value pairs. For example, an application capable of downloading files might use a property named key or value is in the Properties object,; getting the current number of key/value pairs,  Purpose Use to store a numeric value that isn't a monetary value. If you might use the values in the field to perform a calculation, use the Number data type. If you might use the values in the field to perform a calculation, use the Number data type.

How increment a number value property for a , Hi, Does anyone know how to increment the numerical value of a contact property ? Thank you. Increment the value by one, and return the old value: Scope (3) Increment a numerical value: Properties & Relations

Comments
  • Great. This is incrementing the minor version as required. However, the command is only printing the updated things in the console. Whereas I want to update the file.
  • Added the redirect to a temp file and mv command to overwrite the original to the bottom of the question
  • I think you mean this: awk -F"=" 'BEGIN{OFS=FS} $1=="minor"{$2=$2+1}1' versions.properties > versions.properties.tmp && mv versions.properties.tmp versions.properties
  • as there is no tmp file to start with, First awk will operate with original file and write the output to tmp file, then lastly mv will do the rest. Am i right?
  • @Coder Holy smokes yea...! Thanks for that. I have updated.