Use o awk para encontrar a média de uma coluna [duplicada]
Esta questão já tem uma resposta aqui:
awk calcular média ou zero 3 respostasEu estou tentando encontrar a média da segunda coluna de dados usandoawk
para uma aula. Este é o meu código atual, com a estrutura fornecida pelo meu instrutor:
#!/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 estou recebendo um erro que diz:
awk: avg.awk:11: awk 'BEGIN{sum+=$2}' $name
awk: avg.awk:11: ^ invalid char ''' in expression
Eu acho que estou perto, mas eu realmente não tenho idéia de onde ir a partir daqui. O código não deve ser incrivelmente complexo, já que tudo o que vimos na aula tem sido bastante básico. Por favor deixe-me saber.