extact pattern with bash tool

(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"
2

1 Answer

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

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like