awk
クラスを使用して、データの 2 列目の平均を見つけようとしています。これは、インストラクターが提供したフレームワークを使用した現在のコードです。
#!/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
近いと思いますが、ここからどこへ行くべきか本当にわかりません。クラスで見たものはすべてかなり基本的なものなので、コードはそれほど複雑であってはなりません。私にお知らせください。