i got a string

var=96.33% from 120

how do i get 96.33 from $var?

Recommended Answers

All 6 Replies

Regex!
Try something like this:

var='96.33% from 120'
decimal=$(echo $var|grep -Eo '[0-9]*\.[0-9]*')
echo $decimal

I hope this helps!
-G

hi,

$ var="96.33% from 120"
$ echo "${var%\%*}"
96.33

this is POSIX parameter expansion.

as you see, as % is a special character for Parameter Expansion (see man bash), so it has to be protected.

it works,

and now, i want to make a calculation by using the decimal

for instance:

`expr $decimal + 1`

how do i do that?

shells can't do float numbers arithmetic evaluation. expr (that is an external command) can't either.

you'll have to use bc

echo "$decimal + 1" | bc -l

or with bash

bc -l <<<"$decimal + 1"

it works,

and now, i want to make a calculation by using the decimal

for instance:

`expr $decimal + 1`

how do i do that?

it works,

I appreciate all your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.