- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Adding multiple lines up and getting a total
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
06-05-2003 05:02 AM
06-05-2003 05:02 AM
I have a file that looks like this...
45
34
23
What I need to do is add each line up to get a total.
ie for the example above ...
45+34+23 Total = 102
I think I need to use awk for this but cannot remember how.
Any help would be great.
Thanks,
Colin.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 05:05 AM
06-05-2003 05:05 AM
Solutionawk 'BEGIN {total=0;} {total=total+$1;} END {print total,"\n";}'
Rgds...Geoff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 05:06 AM
06-05-2003 05:06 AM
Re: Adding multiple lines up and getting a total
#!/usr/bin/sh
TOTAL=0
for i in $(cat file)
do
let TOTAL=$TOTAL+$i
done
echo "The total is $TOTAL"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 05:11 AM
06-05-2003 05:11 AM
Re: Adding multiple lines up and getting a total
Rgds Jarle
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 05:14 AM
06-05-2003 05:14 AM
Re: Adding multiple lines up and getting a total
perl -lne '{$t+=$_}END{print $t}' filename
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 05:51 AM
06-05-2003 05:51 AM
Re: Adding multiple lines up and getting a total
echo $(($(cat file | tr '\012' '+')0))
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 10:51 AM
06-05-2003 10:51 AM
Re: Adding multiple lines up and getting a total
#!/bin/ksh
for AMOUNT in `cat $FILE`
do
(( TOTAL = "$TOTAL" + "$AMOUNT" ))
done
echo $TOTAL
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2003 11:22 AM
06-05-2003 11:22 AM
Re: Adding multiple lines up and getting a total
number=$(cat file)
e=0
for i in $number
do
((e=e+$i))
done
echo $e
now if you have multiple columns in your file you can just change the value to $number to assign the field you wish to add up.
i.e.
number=$(cat file | awk '{print $2}')
and the 2nd field would be added