计算平均值并将其写入其他文件 [英] Calculate average and write it in other file

查看:49
本文介绍了计算平均值并将其写入其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ID和分数的学生名单,我需要用他们的平均分数做另外一个.main_list:

I have a list of students with ID and marks, and I need to make another one with their average marks. main_list:

#name surname student_index_number course_group_id lecturer_id list_of_marks

athos musketeer 1 1 1 3,4,5,3.5
porthos musketeer 2 1 1 2,5,3.5
aramis musketeer 3 2 2 2,1,4,5


我有这个脚本

awk '{ n = split($6, a, ","); total=0; for (v in a) total += a[v]; print total / n }' main_list

但是我不想打印它,我想将它写在另一个称为平均标记的文件中.最终内容应如下所示,average_list:

But I don't want to print it, I want to write it in other file called average marks. Final content should be like this, average_list:

athos musketeer 1 1 1 3.875
porthos musketeer 2 1 1 3.5
aramis musketeer 3 2 2 3

推荐答案

能否请您尝试一次.

while read first second third fourth fifth sixth
do
  if [[ "$first" =~ (^#) ]]
  then
      continue
  fi
  count="${sixth//[^,]}"    
  val=$(echo "(${#count}+1)" |  bc)
  new_val=$(echo "scale=2; (${sixth//,/+})/$val" | bc)
  echo "$first $second $third $fourth $fifth $new_val"
done < "Input_file"  > "Output_file"

尝试尝试以下操作.

awk  '{ n = split($6, a, ","); total=0; for (v in a) total += a[v]; print $1,$2,$3,$4,$5,total / n }' Input_file  > "Output_file"

这篇关于计算平均值并将其写入其他文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆