Average marks from list

后端 未结 3 1771
北海茫月
北海茫月 2020-12-11 12:13

Sorry if I don\'t write good, it\'s my first post.

I have a list in one file with the name, id, marks etc of students (see below):

And I want to calculate th

相关标签:
3条回答
  • 2020-12-11 12:25

    In pure BASH solution, could you please try following once.

    while read first second third fourth fifth sixth
    do
      if [[ "$first" =~ (^#) ]]
      then
          continue
      fi
      count="${sixth//[^,]}"    
      val=$(echo "(${#count}+1)" |  bc)
      echo "scale=2; (${sixth//,/+})/$val" | bc
    done < "Input_file"
    
    0 讨论(0)
  • 2020-12-11 12:41
    awk 'NR>1{n=split($NF,a,",");for(i=1;i<=n;i++){s+=a[i]} ;print $1,s/n;s=0}' input
    athos 3.875
    porthos 3.5
    aramis 3
    

    For all the lines except header(NR>1 will filter out header) , pick up the last column and split into smaller numbers by comma. Using for loop sum the value of all the marks and then divid by the total subject number.

    0 讨论(0)
  • 2020-12-11 12:42

    Something like (untested)

    awk '{ n = split($6, a, ","); total=0; for (v in a) total += a[v]; print total / n }' main_list
    
    0 讨论(0)
提交回复
热议问题