How do you derive a decimal from a math operation involving a variable?
2
votes
2
answers
896
views
I'm trying to find derive
SCALEFACTOR
which is basically 10000/(sum of 4th column in a file)
. How do I get a decimal from the output? Appreciate any help in advance.
#!/bin/bash
FILES=/path/to/files/*;
for f in ${FILES}
do
echo $f
COLTOTAL="$(awk '{sum += $4} END {print sum}' $f)"
echo "total: ${COLTOTAL}"
# SCALEFACTOR=expr 10^5 / $COLTOTAL
B=10000
SCALEFACTOR=$((B / ${COLTOTAL}))
SCALINGFACTOR=$(echo "100000 / $COLTOTAL" | bc -l
# echo "scale=5; ${SCALEFACTOR}" | bc
echo ${SCALEFACTOR}
awk '{print($1"\t"$2"\t"$3"\t"$4 * ${SCALINGFACTOR})}' $f > $f"_normalized.txt"
done
Asked by Stephen
(141 rep)
Feb 24, 2015, 06:48 PM
Last activity: Feb 17, 2023, 12:41 AM
Last activity: Feb 17, 2023, 12:41 AM