ravi
ravi

Reputation: 39

value not matching while trying to find sum of values in a column using awk

I am trying to find the sum of values(negative & non-negative floating values) in a particular column of a text file and store in variable using awk. Tried this command

bal=`awk -F'|' '{sum += $48} END {printf "%f", sum}' filename`

file is of 15GB size

The output I am getting is 1168475137587.090576, the actual(expected) output should be 1168475137572.33, it is varying by 14.76, looking any better way of calculation for accurate results.

Upvotes: 1

Views: 366

Answers (1)

Ed Morton
Ed Morton

Reputation: 203169

Sounds like you're having a floating point arithmetic issue. You could left-shift by, say, 3 chars (one more precision point than you need for your output apparently), sum using the integers that are the result of that, then right shift again and drop the last char or round however you like:

awk '
    { val=sprintf("%0.3f",$48); sub(/\./,"",val); sum+=val }
    END {sub(/...$/,".&",sum); printf "%0.2f\n", sum }
' file

e.g.:

$ cat file
1
1.1
1.12
1.123
1.1234
1.1239

$ awk '
   {
                                    printf "%s", $1
        val=sprintf("%0.3f",$1);    printf " -> %s", val
        sub(/\./,"",val);           printf " -> %s\n", val
        sum+=val
    }
    END { sub(/...$/,".&",sum); printf "%0.2f\n", sum }
 ' file
1 -> 1.000 -> 1000
1.1 -> 1.100 -> 1100
1.12 -> 1.120 -> 1120
1.123 -> 1.123 -> 1123
1.1234 -> 1.123 -> 1123
1.1239 -> 1.124 -> 1124
6.59

Upvotes: 1

Related Questions