0

(a) 0.2.0-123-g9e17591

(b) 0.2.0-g9e17591

  1. How to get 0.2.0-123 and 0.2.0 respectively ?
  2. How to extract number 0 & 123 from "0.2.0-123-g9e17591" and 2 & 0 from "0.2.0-g9e17591"
stack
  • 1
  • 1
    You can start with this echo "0.2.0-123-g9e17591" | grep -Eo "(^[0-9]+\.)([0-9]+\.)([0-9]+\-)([0-9]+)" and this echo "0.2.0-g9e17591" | grep -Eo "(^[0-9]+\.)([0-9]+\.)([0-9]+)" and please make your question more clear. – Raffa Mar 13 '21 at 03:59
  • Did you find anything posted useful? Please provide feedback. – sancho.s ReinstateMonicaCellio Mar 25 '21 at 17:21

1 Answers1

1

You could use

$ IFS='-' read -ra PARTS <<< 0.2.0-123-g9e17591
$ echo ${PARTS[*]}

to split the string into array PARTS, with - as a field separator, and then check the results.

Then you could proceed similarly with the first element of array PARTS

$ IFS='.' read -ra PARTS2 <<< ${PARTS[0]}
$ echo ${PARTS2[*]}

Repeat the operation as needed.

Sources

  1. https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
  2. http://www.masteringunixshell.net/qa3/bash-how-to-echo-array.html
  3. https://stackoverflow.com/questions/15685736/how-to-extract-a-particular-element-from-an-array-in-bash