Tôi đang cố gắng tìm mức trung bình của cột dữ liệu thứ hai sử dụng awk
cho một lớp học. Đây là mã hiện tại của tôi, với khuôn khổ giảng viên của tôi cung cấp:Sử dụng awk để tìm mức trung bình của một cột
#!/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
}
và tôi nhận được một lỗi mà nói:
awk: avg.awk:11: awk 'BEGIN{sum+=$2}' $name
awk: avg.awk:11: ^invalid char ''' in expression
Tôi nghĩ tôi gần nhưng tôi thực sự không có ý tưởng nơi để đi từ đây. Mã không phải là vô cùng phức tạp như tất cả mọi thứ chúng ta đã thấy trong lớp đã được khá cơ bản. Làm ơn cho tôi biết.
Tôi không phải lo lắng nhiều, nhưng điều này có giúp được không: http://stackoverflow.com/questions/8434000/awk-calculate-average-or-zero?rq=1 –