我正在尝试使用awk
来查找第二列数据的平均值。这是我当前的代码,我的导师提供的框架:使用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没有太多,但这是否有帮助:http://stackoverflow.com/questions/8434000/awk-calculate-average-or-zero?rq=1 –