- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Adding up numbers in a file
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-16-2003 02:41 PM
10-16-2003 02:41 PM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 02:51 PM
10-16-2003 02:51 PM
Re: Adding up numbers in a file
Why don't you use the programming languague that Unix was written in? 'C'?
Here's someones C program:
http://forums1.itrc.hp.com/service/forums/parseCurl.do?CURL=%2Fcm%2FQuestionAnswer%2F1%2C%2C0xa16a46ff9277d511abcd0090277a778c%2C00.html&admit=716493758+1066359015637+28353475
Integer addition :
x=$(($x+1))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 03:18 PM
10-16-2003 03:18 PM
Solution#!/usr/dt/bin/dtksh
typeset -F total=0
cat yourfile |
while read number
total=$(( $total + $number ))
done
print $total
use a program that supports decimal numbers
#!/usr/bin/ksh
cat yourfile |
awk '{
total += $1;
} END {printf("%f\n",total)}'
or bc, perl, write your own
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 05:17 PM
10-16-2003 05:17 PM
Re: Adding up numbers in a file
#/usr/bin/sh
bc |&
TOTAL=0
while read num
do
print -p "$TOTAL + $num"
read -p TOTAL
done < myfile
echo $TOTAL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2003 07:31 PM
10-16-2003 07:31 PM
Re: Adding up numbers in a file
Don't know how many cols your input file has, but assuming each line has 3 cols, each line of which you want totalling, then use:
--
cat yourfile|awk '
{tot1+=$1;tot2+=$2;tot3+=$3}
END {printf "Totals: %f, %f, %f\n", tot1, tot2, tot3}
'
--
This can be extended to the general case.
-- Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2003 04:07 AM
10-17-2003 04:07 AM
Re: Adding up numbers in a file
print -p scale = 2
to get 2 places to the right of the decimal point.