I am writing a bash script on ubuntu11.10
I have some string having numbers and letter and want to add all the numbers together
For example
1s2d23f
I want to perform
1 + 2 + 23
and store it in a variable
I am writing a bash script on ubuntu11.10
I have some string having numbers and letter and want to add all the numbers together
For example
1s2d23f
I want to perform
1 + 2 + 23
and store it in a variable
one quick solution
s=`echo '1s2d23f' | sed 's:[a-zA-Z]:+0:g' | bc`
Here's another using awk:
var=$(echo "1s2d23f" | awk '{gsub(/[[:alpha:]]/, " ");for(i=1;i<=NF;i++) sum=sum+$i;print sum}')
str=1s2d23f
var=$(( ${str//[^0-9]/+}0 ))