gpt4 book ai didi

bash - 使用 awk 求列的平均值

转载 作者:行者123 更新时间:2023-11-29 08:40:51 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 awk 为某个类查找第二列数据的平均值。这是我当前的代码,使用我的讲师提供的框架:

#!/bin/awk

### This script currently prints the total number of rows processed.
### You must edit this script to print the average of the 2nd column
### instead of the number of rows.

# This block of code is executed for each line in the file
{
x=sum
read name
awk 'BEGIN{sum+=$2}'
# The script should NOT print out a value for each line
}
# The END block is processed after the last line is read
END {
# NR is a variable equal to the number of rows in the file
print "Average: " sum/ NR
# Change this to print the Average instead of just the number of rows
}

我收到一条错误消息:

awk: avg.awk:11:        awk 'BEGIN{sum+=$2}' $name
awk: avg.awk:11: ^ invalid char ''' in expression

我想我很接近,但我真的不知道从这里去哪里。代码不应该非常复杂,因为我们在类里面看到的一切都是相当基础的。请告诉我。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com