search

Loading

Friday, May 31, 2013

Print sum of all numbers in a file in linux using awk

This is to print the sum of all numbers in a file using awk.

If the file which we want to process is sum.txt, and it's content is :

cat sum.txt
1
3
2
4
5


 To find the sum of all these numbers we can use below command:



awk '{total+=$0}END{print total} sum.txt

Output will be as below:

awk '{total+=$0}END{print total}' sum.txt
15



No comments :

Post a Comment