- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: sum of 2nd field
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
10-30-2008 07:15 AM
10-30-2008 07:15 AM
sum of 2nd field
I have a big file with numbers in columns. I want to take the sum of the 2nd field in the file.
I used:
cat file|awk '{s=s+$2; print s}'.
But it is giving a exponential value like 6.56616e+09. How can I get the sum in number format?
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 07:26 AM
10-30-2008 07:26 AM
Re: sum of 2nd field
printf
http://www.gnu.org/manual/gawk/html_node/Format-Modifiers.html
http://www.uga.edu/~ucns/wsg/unix/awk/
http://www.unix.com/shell-programming-scripting/77874-awk-printf-formatting-using-string-format-specifier-2.html
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Tags:
- printf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 07:44 AM
10-30-2008 07:44 AM
Re: sum of 2nd field
But it somehow is adding upto 20,000 lines and then subracting the later values I am guessing.
I got 2147483647 from the above command, but 2188720747 when I add in excel.
I am attaching the file for use.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 07:58 AM
10-30-2008 07:58 AM
Re: sum of 2nd field
Or, shorter
awk 'BEGIN{s=0}{s=s+$2}END{printf("%15d\n",s)}' < file
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 08:00 AM
10-30-2008 08:00 AM
Re: sum of 2nd field
HP-Server-Literate since 1979
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 08:19 AM
10-30-2008 08:19 AM
Re: sum of 2nd field
Do some thing like:
# awk 'END{printf("%.0f\n",s)};s+=$2' file
Notice the use of the float specification of 'printf'. Notice too the elimination of the extra 'cat' process.
I also short-handed notation for the auto summation to your 's' variable.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 10:05 AM
10-30-2008 10:05 AM
Re: sum of 2nd field
all small adding to JRF's solutions:
If you do not want to get an echo of each input line (default action in awk), put the summation into an action block instead of the pattern block:
awk 'END{printf("%.0f\n",s)} {s+=$2}' file
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 10:15 AM
10-30-2008 10:15 AM
Re: sum of 2nd field
By the way, if you don't want to see the individual records and sums as you process them, you could do:
# awk 'END{printf("%.0f\n",s)};NF>0 {s+=$2}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 03:06 PM
10-30-2008 03:06 PM
Re: sum of 2nd field
There's no stinkin' integers here. awk uses doubles.
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1267952
>you're "overflowing" into negative values.
You're switching from fixed point output to exponential format because it is too wide for %g. See awk(1):
CONVFMT: Internal conversion format for numbers (default %.6g). If the value of CONVFMT is not a floating-point format specification, the results are unspecified.
>JRF: awk 'END{printf("%.0f\n",s)};NF>0 {s+=$2}' file
If you are going to check NF, why not make sure $2 exists? NF >= 2
Also, I typically put END last as a visual clue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 03:19 PM
10-30-2008 03:19 PM
Re: sum of 2nd field
> Dennis: @ JRF: If you are going to check NF, why not make sure $2 exists? NF >= 2
...because 'awk' would treat absent fields as zero :-;
That aside, Peter's post is a more concise representation of my objective, although I posted before I saw his.
> Dennis: Also, I typically put END last as a visual clue.
Either that way, or at the beginning to high-light the objective of the script as in this case. :-;
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-30-2008 07:38 PM
10-30-2008 07:38 PM
Re: sum of 2nd field
As Ferguson give the right solution I am adding one small thing.
If you are new to printf or you wana add decimal should come with total then do this
Ferguson :
# awk 'END{printf("%.0f\n",s)};NF>0 {s+=$2}' file
Just replace 0 with how many decimal you want
# awk 'END{printf("%.2f\n",s)};NF>0 {s+=$2}' file
Suraj