Re: Strange output format using sum (520 Views)
Posts: 26
Registered: ‎03-25-2002
Message 1 of 7 (551 Views)

# Strange output format using sum

[ Edited ]

Dear friends,

I'm doing a column sum using cat+awk+sum (like below) and the final result comes in a strange format.

cat lixo_avb | awk '{ sum+=\$2} END {print sum}'

1.42881e+06

I think it's have some simple solution, but i try some google searches and i can't find any explanation.

Some help?

Thanks

Wagner
Honored Contributor
Posts: 6,271
Registered: ‎12-02-2001
Message 2 of 7 (545 Views)

# Re: Strange output format using sum

That's a variant of scientific notation that is actually quite common with computers, called "E notation." It's useful for compact representation of very large or very small numbers.

1.42881e+06 means:

1.42881 * (10^6) = 1 428 810

http://en.wikipedia.org/wiki/Scientific_notation

MK
Honored Contributor
Posts: 13,876
Registered: ‎06-21-2000
Message 3 of 7 (543 Views)

# Re: Strange output format using sum

1.42881e+06  means 1.42881 x 10^6 (10 to the 6th power) -- This is standard engineering format and essentially means that you need to move the decimal 6 places to the right.

So 1.42881e+06 = 1,428,810

Posts: 26
Registered: ‎03-25-2002
Message 4 of 7 (540 Views)

# Re: Strange output format using sum

[ Edited ]

Thanks, dudes.

Someone knows any way to force results in a "normal"  output.

Change some parameter, etc...

edit to say...

# echo '1.42881e+06' | awk '{printf "%.0f\n", \$0}'

# 1428810

Well, works fine, but i like to get this result direct from first calc.

Wagner
Acclaimed Contributor
Posts: 25,904
Registered: ‎03-06-2006
Message 5 of 7 (524 Views)

# Re: Strange output format using awk (scientific notation)

[ Edited ]

>but I like to get this result direct from first calc.

Searching the forum should show you many examples.  From awk(1):

awk '

BEGIN { OFMT = "%.0f" }

{ sum+=\$2}

END {print sum}' lixo_avb

This also gets rid of the evil cat.  :-)

Honored Contributor
Posts: 6,271
Registered: ‎12-02-2001
Message 6 of 7 (523 Views)

# Re: Strange output format using sum

The awk function '{printf "%.0f\n", \$0}' does not explicitly mean "convert a number from scientific format to regular one", but simply "output this number in this format". Since awk uses standard C library routines, it understands scientific notation automatically. For awk and any other program that uses the standard scanf functions, scientific notation is freely interchangeable with ordinary number notation, unless the programmer has taken deliberate steps to reject input that uses scientific notation.

So you'll just have to add the output format specification "%.0f" to your original calculation. To do that, you'll need to switch "print" to "printf", and since printf does not output a newline character unless you explicitly request for it (with \n), you'll have to add that, too.

`cat lixo_avb | awk '{ sum+=\$2} END {printf "%.0f\n",sum}'`

MK
Highlighted
Posts: 26
Registered: ‎03-25-2002
Message 7 of 7 (520 Views)

# Re: Strange output format using sum

You guys rock!

Thanks.

Wagner
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.