计算平均值并将其写入其他文件

编程入门 行业动态 更新时间:2024-10-24 10:15:07
本文介绍了计算平均值并将其写入其他文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有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"

更多推荐

计算平均值并将其写入其他文件

本文发布于:2023-10-11 23:26:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   文件   并将其

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!