You could do this with bc
(see the GNU bc manual). You can pass bc
an arithmetical expression and bc
will evaluate it and return the result, e.g.
echo '9.0 - 1' | bc -l
This command returns 8.0
as its result.
Now suppose your version number is contained in the file version.txt
, e.g.:
echo '9.0' > version.txt
Then you could run the following command:
echo "$(cat version.txt) - 1" | bc -l
This would produce 8.0
as its output. Alternatively, suppose that you have a list of version numbers in a file called versions.txt
e.g.:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
We could create such a file like so:
for i in {1..10}; do echo "${i}.0" >> versions.txt; done
In this case we could use bc
inside of a while-loop:
while read line; do echo "${line}-1" | bc -l; done < versions.txt
This produces the following output:
0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
Alternatively, you could extract the integer part from the version number and use bash
to perform the arithmetic. You could use any number of text-processing tools to do this (e.g. grep
, sed
, awk
, etc.). Here is an example using the cut
command:
echo "$(( $(echo '9.0' | cut -d. -f1) - 1)).0"
This produces 8.0
as its output. Putting this into a while-loop gives us the following:
while read line; do \
echo "$(( $(echo ${line} | cut -d. -f1) - 1)).0";
done < versions.txt
4.2.1
? Will the version numbers always be foo dot bar (e.g.2.3
) or can they be more complex? Will the file always be sorted? – terdon Nov 24 '17 at 15:041.0
,1.2
,2.0
and give2.0
as input, do you want1.0
or1.2
as output? And what if you have1.0.1
? – terdon Nov 24 '17 at 15:071
from input numeric string, then, what's the point of using a file at all? Untill you elaborated, using a file is pointless – RomanPerekhrest Nov 24 '17 at 15:084.0
to get3.0
– RomanPerekhrest Nov 24 '17 at 15:17sed
command taking that file as input - that was their solution attempt. – igal Nov 24 '17 at 23:13