AWK sum of large integers

Alex

I am trying to sum a list of integers from a logfile using awk

{sum+=$1} END {print sum}

The problem is that the result is larger than the MAX_INT specified in my limits.h file, so the print returns 3.68147e+09

Is there an elegant way of printing the entire value of the sum?

Thank you!

devnull

You could use bc which supports arbitrary precision arithmetic. The equivalent of what you're trying to achieve would be:

cut -d' ' -f1 inputfile | paste -sd+ | bc -l

EDIT: As per your comment, if you want to prevent splitting of output into multiple lines, set BC_LINE_LENGTH to 0. Say:

cut -d' ' -f1 inputfile | paste -sd+ | BC_LINE_LENGTH=0 bc -l

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related