我想使用 echo|bc 命令来计算大量的计算。例如:
echo "scale=8; sqrt($NUM)" | bc -l
计算 NUM 的平方根到 8 位十进制数字的精度。
现在假设我有一个numbers.txt
包含大量数字的文件,我想计算所有数字的平方根。
我尝试使用
grep -ow "^[0-9]*$" numbers.txt | xargs -I '{}' echo "scale=8; sqrt({})" | bc -l
grep -ow "^[0-9]*$" numbers.txt | xargs -I '{}' (echo "scale=8; sqrt({})" | bc -l)
grep -ow "^[0-9]*$" numbers.txt | xargs -I '{}' $(echo "scale=8; sqrt({})" | bc -l)
read num numbers.txt | echo "scale=8; sqrt($num)" | bc -l
以及这些的更多变体,但找不到使它起作用的方法。有任何想法吗?