Setting Bash variable to last number in output -


i have bash running command program (afni). command outputs 2 numbers, this:

70.0 13.670712

i need make bash variable whatever last # (in case 13.670712). i've figured out how make print last number, i'm having trouble setting variable. best way this?

here code prints 13.670712: test="$(3dbrickstat -mask ../../template/rois.nii -mrange 41 41 -percentile 70 1 70 'stats.s1_ants+tlrc[25]')"; echo "${test}" | awk '{print $2}'

just pipe(|) command output awk. here in example, awk reads stdout of previous command , prints 2nd column de-limited default single white-space character.

test="$(3dbrickstat -mask ../../template/rois.nii -mrange 41 41 -percentile 70 1 70 'stats.s1_ants+tlrc[25]' | awk '{print $2}')" printf "%s\n" "$test" 13.670712 

(or) using echo

echo "$test" 13.670712 

this simplest of ways this, if looking other ways in bash-ism, use read command using process-substitution

read _ va2 < <(3dbrickstat -mask ../../template/rois.nii -mrange 41 41 -percentile 70 1 70 'stats.s1_ants+tlrc[25]') printf "%s\n" "$val2" 13.670712 

another more portable version using set, work irrespective of shell available.

set -- $(3dbrickstat -mask ../../template/rois.nii -mrange 41 41 -percentile 70 1 70 'stats.s1_ants+tlrc[25]'); printf "%s\n" "$2" 13.670712 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -