Thursday, October 6, 2022

[SOLVED] Sum the first column in AWK but gives readable number

Issue

I created the following awk cli in order to sum the first column in the file

example of the file.txt

0
15.5
15.5
15.5
15.2
14.9
14.9
15.2
15.3
15.4
15.5
15.5
15.6
15.6
15.7
15.8
15.8
15.8
15.8
15.8
15.8
.
.
.

but awk gives the number --> 3.70747e+07 , that isn't readable

awk  '{sum+=$1;} END{print sum;}'  file.txt
3.70747e+07

is it possible to change the awk syntax in order to get readable number as 370747.......


Solution

Replace print with printf like:

awk '{sum+=$1;} END{printf("%f\n",sum);}' file.txt


Answered By - UlfR
Answer Checked By - Katrina (WPSolving Volunteer)