Knowing how to sum values in Awk can be extremely useful when working with data. Fortunately, it is very easy. Using variables in Awk will feel different than in other languages. There are no data types in Awk. A variable will either be considered a number or string depending on the context in which it is used.

Here is a simple Awk program that aggregates values in the 11th field of a file, and prints the sum. The parts of this program explained below.

BEGIN{FS="\t"; sum=0}
END{print sum}

BEGIN{FS="\t"; sum=0}
The BEGIN block is only executed once at the beginning of the program. The BEGIN block is often used to set variables. Here were are setting the “FS” variable to indicate that our data is delimited by tabs. We also set the initial value of the variable “sum” to 0.

Here we increment the sum variable by the value in field 11 for each line.

END{print sum}
The END block is only executed once at the end of the program. Here we simply print the value of our “sum” variable.

Examples of the program being run with a file in the Big Datums GitHub repo shown below:

# cat the file and pipe the output to Awk
cat bddatagen_people_wHeader_v1_5k.txt | awk 'BEGIN{FS="\t"; sum=0} {sum+=$11} END{print sum}'

# save the commands as an Awk script.  Execute the script and pass the file name as an argument
awk -f sum.awk bddatagen_people_wHeader_v1_5k.txt 

# cat the file and pipe output to Awk script
cat bddatagen_people_wHeader_v1_5k.txt | awk -f sum.awk

Leave a Reply

How to Sum Values in Awk