Recently, I had to sum up all integers of a column in a text file (similar to how you do in excel). After some digging up, I came up with a
awk one liner to do it.
Input and output data
Here is the super simplified version of input data that I was using.
I wanted to find the sum of all values present in the 3rd column. So in the case, the output that I was expecting was
Here is the awk one liner, which does this.
awk script is executed for each line and the first part of the command creates a variable
s that stores the sum of all values in the 3rd column.
When the end of file is reached, the second part of the command is executed, which just prints the value of the variable.
If the columns are separated by a comma or by any other non whitespace character, then you have to just specify it by adding
FS=',' to the above command.
The more I dig deeper into
awk, the more I like it and it is really fascinating to see how much you can do with this tool.
I learned a lot about
awk and hopefully this teaches something to you as well