## awk printf problem

awk printf problem
# 1
awk printf problem

Hi Friends,

Can anyone guide me how to compute sum of column4 from the below file x using awk command? when i do using awk I'm getting sum 7482350198352648.000000 which is not accurate.

\$ cat x
56,232,dfgjkhdfj,,56,anand
56,22,dfgjkhdfj,7482347823453123.97834 ,56,Khan
56,23,dfgjkhdfj, ,56,krishna
56,232,dfgjkhdfj,45.68 ,56,Hari
56,232,dfgjkhdfj,2374899477.99235 ,56,Gopa
56,232,dfgjkhdfj, ,56,Gopa

\$ echo 7482347823453123.97834+45.68+2374899477.99235|bc -l
7482350198352647.65069 ===> accurate result

\$ awk -F, '{a+=\$4} END {printf("%20.6f\n",a)}' x
7482350198352648.000000 ===> why I'm not getting correct result?

Krishna
 krishna View Public Profile for krishna Find all posts by krishna
# 2
awk uses double precision floating point. There is a limit to its accuracy and your data exceeds that limit.
 Perderabo View Public Profile for Perderabo Find all posts by Perderabo
# 3
Re: awk printf problem

Perderabo,

Can you tell me what is the maximum number it can hadle
example 99999999999999999999.999999 ???
 krishna View Public Profile for krishna Find all posts by krishna
# 4
It varies. On my Sun it's 1.7976931348623157E+308

But do a "man limits" or find your <limits.h> and look up your value for DBL_MAX.
 Perderabo View Public Profile for Perderabo Find all posts by Perderabo
# 5
I've checked the setting is
#define DBL_MAX 1.79769313486231570e+308

what I want to ask is how many total digits can hadled by awk?
 krishna View Public Profile for krishna Find all posts by krishna
# 6
It can handle 308 digits. What you're looking for is a guarantee of n digits of integer-like arithmetic accuracy. Floating point arithmetic doesn't behave like that. Don't try to squeeze integer arithmetic behavior out of floating point. If you need integer arithmetic, simply switch to integer arithmetic. You already have the solution to your problem: use bc.

There probably is some number of digits of integer arithmetic style accuracy to had from floating point if you limit yourself to the addition of positive integers (something you were not doing). I don't know what that limit is. Nor do I care. But as soon as you start doing stuff like 9999.9999 you leave floating point behind very quickly. Not all decimal rationals can even be represented in floating point.
 Perderabo View Public Profile for Perderabo Find all posts by Perderabo
# 7
Friend,

\$ cat x
56,232,dfgjkhdfj,,56,anand
56,22,dfgjkhdfj,7482347823453123.97834 ,56,Khan
56,23,dfgjkhdfj, ,56,krishna
56,232,dfgjkhdfj,45.68 ,56,Hari
56,232,dfgjkhdfj,2374899477.99235 ,56,Gopa
56,232,dfgjkhdfj, ,56,Gopa

\$ awk -F, '{a+=\$4} END {printf("%20.6f\n",a)}' x
7482350198352648.000000 ===> why I'm not getting correct result?

what is the equivalent bc command for the above awk command?

 krishna View Public Profile for krishna Find all posts by krishna

Test Your Knowledge in Computers #253
Difficulty: Easy
The Defense Advanced Research Projects Agency (DARPA) is an agency of the United States Department of Defense originally designed by the Jefferson Airplane Company.
True or False?

## problem with printf in shell script

i have written small script as follows: name="hi hello" printf "%-20s" \$name This gives me strange output. -20s format is applied on both word of string. i.e it displays both word hi and hello in space of 20 length. I want to display entire string "hi hello" in length of 20 space. plz...

## awk with printf

Hi, I am using the following code to assign a count value to a variable. But I get nothing. Do you see anything wrong here. I am new to all this. \$CTR=`remsh \$m -l \$MACHINES{\$m} -n cat \$output | grep -v sent | grep \"\$input\" | sort -u | awk '{print \$5}'`; Upto sort - u it's...

## printf problem

In one of the scripts I am using pintf function as following printf "%s%s%s\n" "\$f1" "\$f2" "\$f3" f3 variable contains a string of 10 characters. However it has value first 7 character and last 3 characters are empty. Example Aaaaaaa<3 spaces> bbbbbbb<3 spaces> ccccccc<3 spaces>...

## Printf problem

I am having a major problem with printf, The more I pad it, the less I see :( The problem is in the first function, report Am I ruining output somewhere? I wont print out the names propely, it cuts them off or deletes them completely :( #!/bin/bash report() { printf "%-10s" STUD# ...

## IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name...

## awk and printf

echo \$bbsize 1.5 echo \$fillpercent .95 echo \$bbsize | awk '{printf "%.2f\n",\$0*\$fillpercent}' 2.25 echo \$bbsize | awk '{printf "%.2f\n",\$0*.95}' 1.42 1.42 is what I'm expecting... echo \$blocksize 4096 echo \$bbsize | awk '{printf "%.2f\n",\$0*\$blocksize}' 2.25 echo \$bbsize |...

## Awk printf problem

Hi, I've got a basic problem using printf statement in awk. I want to write float values with always 8 characters width. Examples : 1.345678 12.45678 123.4567 1234.678 -23.5678 -2.45678 -23456.8 ..... I cannot find the right printf format %8.1f, %7.5f.... Can anyone help ?...

## printf problem

I have the following code: \$ awk '{ printf "%-10s %s\n", \$1, \$2, \$3, \$4, \$5, \$5, \$6 }' file i can only print the first 2 elements (\$1,\$2). How can i print all the elements to appear like this: aardvark 5555553 jhfjhfjkg efiigig ejkfjkej wjkdjk alpo-net 5553412 ...

## perl: printf indentation problem

hi all, im having a problem with using perl printf. my requirement is to print a string (like ) at the right most end of the screen. i tried this perl script, but it fails with an error; #!/usr/bin/perl use strict; use warnings; my \$scrW = 0; my \$str = `stty size`; # get the...

## disturbing problem with PRINTF() !!

hello everybody, here is my problem: ________________________________________ #include <stdio.h> int main() { int i=10; printf("value is %i",i); return 0; } _________________________________________ when i compile and execute, nothing appears on screen!! but if i replace the printf...