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.
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.
The END block is only executed once at the end of the program. Here we simply print the value of our “sum” variable.
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.
{sum+=$11}
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:
0 comments:
Post a Comment