Help with average calculation.


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with average calculation.
# 1  
Old 05-05-2019
Help with average calculation.

i have a file with 2 columns. i want to calculate the average of column 1 based on the values of column 2. here's how the file looks like. i want to calculate the sums of numbers corresponding to 1 and then calculate the average. same for numbers corresponding to zero. any help with a code would be appreciated.
Code:
0         1
1          1
2          1   
3         1
4          1
5          1
6          1
7          0
8          0
9          0
10        0
11         0
12         1  
13         1
14         1
15         1
16        0
17        0
18        0


Last edited by RavinderSingh13; 05-06-2019 at 03:39 AM..
# 2  
Old 05-05-2019
Try
Code:
awk '{SUM[$2] += $1; CNT[$2]++} END {print SUM[0] / CNT[0], SUM[1] / CNT[1]}' file
12 6.81818

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculation

Hi, i have a large file like this: Contig1 1 5 Contig1 2 4 Contig1 3 3 Contig1 4 5 Contig1 5 3 Contig1 6 4 Contig2 1 3 Contig2 2 7 Contig2 3 2 Contig2 4 9 Contig2 5 10 Contig2 6 3 Contig2 7 7 Contig2 8 2 Contig2 9 7 Contig2 10 5 contig1 2 4 contig1 3 3 contig1 4 5 (3 Replies)
Discussion started by: the_simpsons
3 Replies

2. Shell Programming and Scripting

Size calculation MB to GB

pcmpath query device |awk 'BEGIN{print "TYPE\tDEVICE NAME\tSERIAL\tSIZE\tHOSTNAME"} /DEVICE/ { disk=$5 printf "%s\t", $7 printf "%s\t", disk getline; printf "%s\t", substr($2, length($2)-3) ("bootinfo -s " disk) | getline; printf... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

3. Shell Programming and Scripting

Date calculation

Hi I have this date 11:30:02-31.11.12 How to calculate date between this date and now? I like it printed like this: 31D 2H 1M if possible. (6 Replies)
Discussion started by: Jotne
6 Replies

4. Shell Programming and Scripting

VG calculation in GB

for i in `lsvg` do echo "VG Name:" $i echo "Total VG Size:" lsvg $i |grep "TOTAL PPs:" |awk '{print $7}' | cut -c2- echo "Free VG Size:" lsvg $i |grep "FREE PPs:" | awk '{print $7}' | cut -c2- done The PP Sizes are in MB. I like to have the sizes in GB. Also, I like to have the... (14 Replies)
Discussion started by: Daniel Gate
14 Replies

5. 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

6. Shell Programming and Scripting

calculation

Could someone till me what this calculation really means let foo=`date "+(1%H-106)*60+1%M-100"` bar=foo+1440 (4 Replies)
Discussion started by: freddie999
4 Replies

7. Shell Programming and Scripting

Memory Calculation

Hi, I want an script that calculate total memory consume by all Oracle Process 6689 oraprod 16 0 1163m 155m 150m S 0 2.0 0:01.95 oracle 7046 oraprod 16 0 1161m 18m 14m S 0 0.2 0:00.02 oracle 7392 oraprod 16 0 1165m 39m 33m S 0 0.5 0:00.07 oracle 7394 oraprod 16 0 1161m 22m 18m S... (1 Reply)
Discussion started by: telnor
1 Replies

8. Shell Programming and Scripting

summery calculation

Hi All I want to make summery for Date=245Duration=545 ... (1 Reply)
Discussion started by: nalakaatslt
1 Replies

9. Shell Programming and Scripting

calculation

Hi, I am in ksh88 I am trying to get the result of the calculation using 3 variables: TOTAL CAPACITY and get the following error: $DB_CAPACITY=(( $DB_SIZE * 100 / $TOTAL )) ksh: syntax error: `((' unexpected I cannot figure out what am I doing wrong... Thanks for any help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

10. UNIX for Dummies Questions & Answers

Average completion time calculation?

I've been trying all night to come up with a script that will take a file that contains job completion times like this as input: 18:30 17:45 16:39 18:01 17:50 ... and figure the Average completion time. I've tried several things, and I just can't seem to get it to figure correctly. I'm... (5 Replies)
Discussion started by: Seawall
5 Replies
Login or Register to Ask a Question