0

I have strings that look like this:

user@pc: $ example="bla bla bla (m=100, number of steps=1.05)"

How can I read the values 100 and 1.05 into two variables that I can use them afterwards?

user@pc: $ echo $A
100
user@pc: $ echo $B
1.05
1

2 Answers 2

1

Use an extra tool like grep to extract numbers first:

NUMS=($(grep -Eo '[0-9]+(\.[0-9]+)?' <<<"${example}"))
A=${NUMS[0]}
B=${NUMS[1]}
Sign up to request clarification or add additional context in comments.

Comments

0

Another way with bash

example="bla bla bla (m=100, number of steps=1.05)"
var=(${example//[^0-9 \.]})
for i in ${var[@]};do
  echo $i
done

1 Comment

Not quite effective in certain cases: example="123aaa456bbb789

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.