How to use awk tool.

Hi all,

Today, through this post I am going to talk about some of the basics of the awk tool and real life case scenarios where this tool is very useful.

awk -F, '{sum+=NF} END {print sum}' <csv file> - Counts the total number of comma separated values in the csv file.

Awk command to calculate sum of floating point numbers in a column stored in a file.

awk '{sum+=sprintf("%f",$1)}END{printf "%.6f\n",sum}' <filename>
LC_NUMERIC="C" awk '{sum += $1} END {print sum}' <filename>

How to add line numbers using awk command?

awk '{printf("%1d,%s\n", NR,$0)}' filename > filename_numbered