使用 bc 处理多个数据源

more than one source of data to process with bc

我有这个文件,其中一列包含数字:

[root@server1]# cat numbers.txt
30
25
15

我需要将它们加在一起,所以我这样做了:

[root@autonoc cattests]# cat numbers.txt |  paste -s -d+ | bc
70

但是在我将它们加在一起之后我需要将它们除以 60,就像这样:

[root@server1]# cat numbers.txt |  paste -s -d+ | "new command" | bc

我该怎么做?

bc -l <<< '('$(paste -s -d+ numbers.txt)')/60'

使用 awk:

$ awk '{s+=} END{print s/60}' numbers.txt
1.16667

工作原理

  • s+=

    numbers.txt的每个留置权的数字被添加到变量s

  • END{print s/60}

    读取完文件后,我们打印 s 除以 60 的值。

awk -v div=60 '{tot+=[=10=]}END{print tot/div}' numbers.txt

使用 -v div=60 可以进一步扩展以接受任何用户输入,例如

read -p "Enter the div value: " div
awk -v div="$div" ...

IHTH

你可以使用 dc

dc -f numbers.txt -e '++3k60/[result = ]np'