3
Contributors
6
Replies
24
Views
4 Years
Discussion Span
Last Post by old_apache
0

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

0

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.

0

it works,

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

for instance:

`expr $decimal + 1`

how do i do that?

0

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"
0

it works,

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

for instance:

`expr $decimal + 1`

how do i do that?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.