- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk sum script
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
Discussions
Discussions
Forums
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
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
тАО01-07-2010 04:55 PM
тАО01-07-2010 04:55 PM
Awk sum script
1
2
3
I would like to use awk to sum up the total of these numbers and output the number 6.
Is this possible in awk?
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-07-2010 05:08 PM
тАО01-07-2010 05:08 PM
Re: Awk sum script
# cat a1
1
2
3
# awk 'BEGIN {sum=0}{sum+=$1} END {print sum}' < a1
6
Hope this helps..!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-07-2010 07:07 PM
тАО01-07-2010 07:07 PM
Re: Awk sum script
abc is file name.
BR,
Kapil+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2010 01:10 AM
тАО01-08-2010 01:10 AM
Re: Awk sum script
awk(1) has an example that does that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2010 07:54 AM
тАО01-08-2010 07:54 AM
Re: Awk sum script
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2010 08:14 AM
тАО01-08-2010 08:14 AM
Re: Awk sum script
typeset i sum
typeset i incr
sum=0
echo $sum
while read incr;
do
let sum=$sum+$incr
echo $sum
done < yourfile
there are also methods involving bc that will do reals/floats
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2010 08:54 AM
тАО01-08-2010 08:54 AM
Re: Awk sum script
You may want to just use:
(( sum += incr ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2010 09:16 AM
тАО01-08-2010 09:16 AM
Re: Awk sum script
> Thanks so much for everyone's help. I'm also considering doing the sum in ksh/bash only...Is this possible?
Yes, as OldSchool showed you, the shell can do this. Beware that in HP-UX, the Posix ('/usr/bin/sh') shell limits you to 32-bit arithmetic. If you are running 11.23 or later, the Korn shell ('/usr/bin/ksh') will support 64-bit arithmetic.
Running OldSchool's code and adding the numbers found in an input file:
1
2147483648
2
3
...yields:
-2147483642
Changing to a Korn shell yields the correct value:
2147483654
Also:
typeset i sum
...should be:
typeset -i sum
Regards!
...JRF...