Find the sum of numbers in a text file

If we have a text file which contains numbers along with other data and we have to find the sum of those numbers , then AWK
would come in handy.



If the file contains above data , we could use AWK to find the sum of the number as specified in the below onliner script

awk -F"," '{SUM+=$1} END{print SUM}'  file.txt

-F”,” : Specify comma as the delimiter
$1 : contains the first field, which is the number in our case
SUM : Variable storing the sum
END{print SUM}: The code within the END block will be executed after processing is completed on the input data, here we are printing the output

Leave a Reply

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