[awk] Calculate an average

Created on November 28, 2026

This example calculates an average with awk.

First, create a file named dummy.txt with these contents.

10
20
50
20

Run the following command. It adds the first value on each line to total. At the end, print total/NR divides the total by the number of lines and prints the result.

cat dummy.txt | awk '{total+=$1}END{print total/NR}'

The command produces the following output.

25
awk Shell command