1

I am trying grep only the numbers from the end of the string until any other char, so from example: "Version 1.2.34" Will give me '34' to variable $minor and 'Version 1.2.' to variable $type.

Here's what I've tried:

minor=$(grep -Eo '[0-9]{1,24}')

but this gives me ALL numbers.

starball
  • 20,030
  • 7
  • 43
  • 238
pizza_pasta
  • 87
  • 1
  • 7

1 Answers1

1

Using pure bash:

s="Version 1.2.34"

minor="${s##*[!0-9]}"
type="${s%$minor}"

declare -p type minor
declare -- type="Version 1.2."
declare -- minor="34"
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hey, it works in Bash but not in Jenkinsfile: ```WorkflowScript: 62: unexpected char: '#' @ line 62, column 29. minor=sh"${s##*[!0-9]}"```. any idea why? – pizza_pasta Apr 30 '22 at 11:50
  • Sorry there was no mention of Jenkinsfile. May be you are not using `bash` in your Jenkinsfile. [Check this answer for using bash interpreter](https://stackoverflow.com/a/45179743/548225) – anubhava Apr 30 '22 at 11:59
  • 1
    thx, I will check it :) – pizza_pasta Apr 30 '22 at 12:10