Code:
set -- `echo "$string" | sed -e 's/[^0-9][^0-9]*/ /g'`
echo First value is $1
echo Second value is $2
The
set -- `command` idiom is rather obscure, but it's nevertheless a standard technique for splitting whitespace-separated tokens into the shell's positional variables. After the set, the first token in the output from
command will be in
$1, the second in
$2, etc, and
$# will tell you how many there were, just like when a script is invoked with command-line parameters.
If this is too weird for you then try the following.
Code:
variable1=`echo "$string" | sed -e 's/^[^0-9]*\([0-9][0-9]*\).*/\1/'`
variable2=`echo "$string" | sed -e 's/.*\([0-9][0-9]*\)[^0-9]*$/\1/'`
The first will grab the first number in the string, and the second, the last. If there are more, they will be lost.