Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a character from string

Tags:

linux

bash

sed

awk

cut

I'm simply trying to remove a % sign from a variable, so I may perform mathmatic calculations on the result. I've tried several things, but none of them seem to work:

$ output="23%"
# usep=$(echo $output | awk '{print $1}' | cut -d '%' -f 1)
# usep=`echo $output | sed 's/\%//g`
# usep=`echo $output | head -c 2`

The variable usep still contains the % character. Any ideas?

like image 558
user1930517 Avatar asked Feb 21 '26 10:02

user1930517


1 Answers

You may use POSIX parameter substitution:

$ output="23%"
$ usep=${output%%\%}
$ echo $usep
23
like image 69
Rubens Avatar answered Feb 24 '26 03:02

Rubens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!