Calculating the Number of Rows and Average


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Calculating the Number of Rows and Average
# 1  
Old 08-15-2008
Calculating the Number of Rows and Average

Hi All

I like to know how can we calculate the number of rows and the average of the values present in the file. I will not know what will be the rowcount, which will be dynamic in nature of the file.

eg.

29
33
48
30
28
# 2  
Old 08-15-2008
Code:
awk '{ sum+=$1
         rowcnt++}
       END {printf("average: %.2f  rowcount:%d", sum, rowcnt) }' inputfile

# 3  
Old 08-15-2008
This is untested code..

nawk 'NR==1{k=0;Sum=0}{print $1; k=k+1; Sum=Sum+$1}END{print "Raws=" k " AVG.= "Sum/k} inputfile
# 4  
Old 08-15-2008
Thanks...This works
awk 'NR==1{k=0;Sum=0}{print $1; k=k+1; Sum=Sum+$1}END{print "Raws=" k " AVG.= "Sum/k}' input File

But what iam looking for is
Output

<Name of the service> <AverageValue> <Number of Rows>
# 5  
Old 08-15-2008
could you provide us with example.... I dont really understand your request.. what do u mean by name of the service...
# 6  
Old 12-11-2008
Hi Jim, what does %.2f mean?
# 7  
Old 12-11-2008
Quote:
Originally Posted by grossgermany
Hi Jim, what does %.2f mean?
display two places after decimal point
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating average from files

I have some files with the following contents.I would like to calculate average of fifth column. How can I do this with awk? file1 cat 95.9 152 78.0 17.9 rat 67.1 153 36.5 30.6 dog 81.4 154 68.1 13.3 dog 92.0 155 55.5 36.5 rat 73.8 156 23.9 49.9 file2 rat... (4 Replies)
Discussion started by: avina
4 Replies

2. UNIX for Dummies Questions & Answers

Calculating average

Hi I have file like below 111,victor,48,12,36 342,Peter,54,58,30 476,Scott,25,36,48 567,Patty,74,17,95 I have written below code to calcualte avereage for every id Victor = 48+12+36/3 #!/bin/ksh /usr/xpg4/bin/awk ' BEGIN {FS=","} {sum=0; n=0;i=3 (1 Reply)
Discussion started by: stew
1 Replies

3. Shell Programming and Scripting

Calculating the average of scores

Hi I have 2 files file1 aac 23 25 aac 87 90 aac 33 67 file2 23 0.9 24 0.8 25 0.4 ........ 67 0.55 ........ I want to get output as (11 Replies)
Discussion started by: anurupa777
11 Replies

4. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

5. UNIX for Dummies Questions & Answers

Calculating weighted average

Dear all, i have 200 values in a file. How can i calculate a weighted average and output into a new file avg.dat? INPUT: file1.dat 1.3453 2.434 2.345 ..... OUTPUT: avg.dat file1: 1.762 Thanks. Po (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

6. UNIX for Dummies Questions & Answers

Calculating average

Hi, i have 12 float variables in a bash file and i want to calculate the average of them. Can any body help? (6 Replies)
Discussion started by: limadario
6 Replies

7. Shell Programming and Scripting

Average calculation based on number of rows

Dear users, I need your support, I have a file like this: 272134.548 6680572.715 272134.545 6680572.711 272134.546 6680572.713 272134.548 6680572.706 272134.545 6680572.721 272134.543 6680572.710 272134.544 6680572.715 272134.543 6680572.705 272134.540 6680572.720 272134.544... (10 Replies)
Discussion started by: Gery
10 Replies

8. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows?

Hi Friends, In continuation to my earlier post https://www.unix.com/shell-programming-scripting/99166-script-find-average-given-column-also-specified-number-rows.html I am extending my problem as follows. Input: Column1 Column2 MAS 1 MAS 4 ... (2 Replies)
Discussion started by: ks_reddy
2 Replies

9. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows??

Hi friends I have 100 files in my directory. Each file look like this.. Temp1 Temp2 Temp3 MAS 1 2 3 MAS 4 5 6 MAS 7 8 9 Delhi 10 11 12 Delhi 13 14 15 Delhi 16 17 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

10. Shell Programming and Scripting

Calculating the average

This is the cronjob ---------------------- root@a7germ:/home/paxtemp > crontab -l|grep test 57 * * * * /home/paxtemp/test_1.sh 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/paxtemp/test.sh root@a7germ:/home/paxtemp > This is the contents of test.sh script... (2 Replies)
Discussion started by: kekanap
2 Replies
Login or Register to Ask a Question