Thursday, October 6, 2022

[SOLVED] Extract version number from file in shell script

Issue

I'm trying to write a bash script that increments the version number which is given in

{major}.{minor}.{revision}

For example.

1.2.13

Is there a good way to easily extract those 3 numbers using something like sed or awk such that I could increment the {revision} number and output the full version number string.


Solution

$ v=1.2.13
$ echo "${v%.*}.$((${v##*.}+1))"
1.2.14

$ v=11.1.2.3.0
$ echo "${v%.*}.$((${v##*.}+1))"
11.1.2.3.1

Here is how it works:

The string is split in two parts.

  • the first one contains everything but the last dot and next characters: ${v%.*}
  • the second one contains everything but all characters up to the last dot: ${v##*.}

The first part is printed as is, followed by a plain dot and the last part incremented using shell arithmetic expansion: $((x+1))



Answered By - jlliagre
Answer Checked By - Clifford M. (WPSolving Volunteer)