【发布时间】:2013-10-09 14:13:56
【问题描述】:
我正在尝试使用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
我想我已经很接近了,但我真的不知道从这里去哪里。代码不应该非常复杂,因为我们在课堂上看到的一切都是相当基本的。请告诉我。
【问题讨论】:
-
我不太了解 awk,但这有帮助吗:stackoverflow.com/questions/8434000/…