- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: about the shell 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
тАО10-26-2003 09:11 PM - last edited on тАО03-13-2015 01:17 AM by Maiko-I
тАО10-26-2003 09:11 PM - last edited on тАО03-13-2015 01:17 AM by Maiko-I
about the shell script
I have a text file in shell , how to calculate the average of the it.
#vi abc.txt
10
20
30
40
I want to have the result 25 (100/4) , can suggest how to write the script ? thx.
P.S. This thread has been moved from HP-UX > System Administration to HP-UX > languages. - Hp ForumModerator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-26-2003 09:17 PM
тАО10-26-2003 09:17 PM
Re: about the shell script
cat abc.txt|awk '
{total+=$1}
END {printf "Average = %f\n", total/NR}
'
-- Graham
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-26-2003 10:48 PM
тАО10-26-2003 10:48 PM
Re: about the shell script
you can try the following:
sum=0
avr=0
for i in `cat abc.txt`
do
sum=`expr $sum + $i`
done
avr=`expr $sum / 4`
echo $avr
NB: use the right single quote "`" NOT "'"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-26-2003 10:59 PM
тАО10-26-2003 10:59 PM
Re: about the shell script
lines=0
total=0
avg=0
cat abc.txt|while read LINE
do
lines=$(expr $lines + 1)
total=$(expr $total + $LINE)
done
avg=$(expr $total / $lines)
echo "Total : "$total
echo "Records: "$lines
echo "Average: "$avg
--Jim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-26-2003 11:36 PM
тАО10-26-2003 11:36 PM
Re: about the shell script
Try this
cat abc.txt |grep "^$" |awk '{a+=$NF} END {print a/NR}'
This will give u avg of n numbers.
grep "^$" is used to filter blank lines if
any otherwise which will be also counted as NR.(number of records).
check it out.
bye
baiju.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2003 01:11 AM
тАО10-27-2003 01:11 AM
Re: about the shell script
awk solution seems to be the simpliest one ... but just for your information, doing arithmetic operation is also simple in shell :
#!/usr/bin/sh
typeset -i TOT VAR NB
cat abc.txt | while read VAR
do
((TOT=TOT+VAR))
((NB=NB+1))
done
print $((TOT/NB))
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2003 08:27 PM
тАО10-27-2003 08:27 PM
Re: about the shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2003 08:31 PM
тАО10-27-2003 08:31 PM
Re: about the shell script
awk 'BEGIN {max=0;total=0}
{ total += $1; if (max < $1) {max=$1}}
END {printf {"Average: %d \t Maximum: %d\n",total/NR,max);}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2003 08:57 PM
тАО10-27-2003 08:57 PM
Re: about the shell script
#!/usr/bin/sh
typeset -i TOT VAR NB MAX
cat a | while read VAR
do
((TOT=TOT+VAR))
((NB=NB+1))
[[ VAR -gt MAX ]] && ((MAX=VAR))
done
print "Average : $((TOT/NB)) Max : $MAX"
Regards.