- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Strange output format using sum
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 09:21 AM - edited 03-16-2012 09:22 AM
03-16-2012 09:21 AM - edited 03-16-2012 09:22 AM
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
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 09:45 AM
03-16-2012 09:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 09:47 AM
03-16-2012 09:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 09:54 AM - edited 03-16-2012 10:16 AM
03-16-2012 09:54 AM - edited 03-16-2012 10:16 AM
Re: Strange output format using sum
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 11:52 AM - edited 03-16-2012 12:10 PM
03-16-2012 11:52 AM - edited 03-16-2012 12:10 PM
Re: Strange output format using awk (scientific notation)
>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. :-)
- Tags:
- evil cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 11:54 AM
03-16-2012 11:54 AM
SolutionThe 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}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2012 11:58 AM
03-16-2012 11:58 AM
Re: Strange output format using sum
You guys rock!
Thanks.