Rodrigo
Rodrigo

Reputation: 12683

Evaluate variable to integer with sed

This can be so easy to a people who know. I am almost finishing this command

echo VERSION=1.0 | sed 's/^VERSION=\([0-9]\).\([0-9]\)/VERSION=\1.\2+1/'

I only want to write VERSION=1.1 . How can I evaluate \2 to integer and sum +1..

Upvotes: 1

Views: 1611

Answers (3)

chown
chown

Reputation: 52738

You can use the bc command:

echo VERSION=`echo "1.0 + 0.1" | bc`

Results in:

VERSION=1.1

man bc

echo "VERSION="`echo "v=1.0; v+=0.1; v" | bc` > myFile.txt
cat myFile.txt

VERSION=1.1

Upvotes: 0

Kent
Kent

Reputation: 195079

of couse sed can do that. that's what e for. you can pass matched/replaced string to shell command using "e"

see the example based on your sed line:

kent$  echo VERSION=1.0 | sed 's/^VERSION=\([0-9]\).\([0-9]\)/echo "VERSION=\1.$((\2+1))"/e'                                             
VERSION=1.1

Upvotes: 2

Karoly Horvath
Karoly Horvath

Reputation: 96266

Crpytic answer - how to use the whole toolkit:

x='VERSION=1.0'
echo -n $x | sed 's/\..*/./'; expr `echo $x | grep -o '\..*' | cut -c 2-` + 1

Upvotes: 1

Related Questions