bash- find average of numbers in line

前端 未结 4 1283
我寻月下人不归
我寻月下人不归 2021-01-22 01:54

I am trying to read a file line by line and find the average of the numbers in each line. I am getting the error: expr: non-numeric argument

I have narrowe

4条回答
  •  情歌与酒
    2021-01-22 02:54

    Some variations on the same trick of using the IFS variable.

    #!/bin/bash
    
    while read line; do
        set -- $line
        echo $(( ( $(IFS=+; echo "$*") ) / $# ))
    done < rows
    
    echo
    
    while read -a line; do
        echo $(( ( $(IFS=+; echo "${line[*]}") ) / ${#line[*]} ))
    done < rows
    
    echo
    
    saved_ifs="$IFS"
    while read -a line; do
        IFS=+
        echo $(( ( ${line[*]} ) / ${#line[*]} ))
        IFS="$saved_ifs"
    done < rows
    

提交回复
热议问题