How to Sum Values in Awk

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}
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:

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">