How to sum up every 10 lines and calculate average using AWK?

后端 未结 2 1533
轮回少年
轮回少年 2021-01-02 05:18

I have a file containing N*10 lines, each line consisting of a number. I need to sum up every 10 lines and then print out an average for every such group. I know it\'s doabl

2条回答
  •  猫巷女王i
    2021-01-02 05:35

    Try something like this:

    $ cat input 
    1
    2
    3
    4
    5
    6
    2.5
    3.5
    4
    $ awk '{sum+=$1} (NR%3)==0{print sum/3; sum=0;}' input 
    2
    5
    3.33333
    

    (Adapt for 10-line blocks, obviously.)

提交回复
热议问题