我正在嘗試使用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 –