-1

I have this file that contains number in a column:

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

I need to add them together so I do this:

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

But after I add them together I need to divide them by 60,something like this:

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

how can I do that?

choroba
  • 231,213
  • 25
  • 204
  • 289
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Dec 19 '17 at 21:37

4 Answers4

3

Using awk:

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

How it works

  • s+=$1

    The number on each lien of numbers.txt is added to variable s.

  • END{print s/60}

    After we have finished reading the file, we print the value of s divided by 60.

John1024
  • 109,961
  • 14
  • 137
  • 171
2
bc -l <<< '('$(paste -s -d+ numbers.txt)')/60'
choroba
  • 231,213
  • 25
  • 204
  • 289
0
awk -v div=60 '{tot+=$0}END{print tot/div}' numbers.txt

Using the -v div=60 can be extended further to accept any user input, like

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

IHTH

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
shellter
  • 36,525
  • 7
  • 83
  • 90
0

You can use dc

dc -f numbers.txt -e '++3k60/[result = ]np'
ctac_
  • 2,413
  • 2
  • 7
  • 17