Sto tentando di trovare la media della seconda colonna di dati utilizzando awk
per una classe. Questo è il mio codice corrente, con il quadro fornito il mio istruttore:Utilizza awk per trovare la media di una colonna
#!/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
}
e sto ottenendo un errore che dice:
awk: avg.awk:11: awk 'BEGIN{sum+=$2}' $name
awk: avg.awk:11: ^invalid char ''' in expression
Credo di essere vicino, ma io in realtà non hanno alcuna idea di dove per andare da qui. Il codice non dovrebbe essere incredibilmente complesso in quanto tutto ciò che abbiamo visto in classe è stato abbastanza semplice. Per favore mi faccia sapere.
Non sono molto in attesa, ma questo aiuto: http://stackoverflow.com/questions/8434000/awk-calculate-average-or-zero?rq=1 –