average rank order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting average rank order
# 1  
Old 11-16-2011
average rank order

Hi,

I have a file with 2 columns

Code:
ABC  6
ABC 22
ABC 44
ABC 56
XYZ 12
XYZ 23
XYZ 42
DEF  2
DEF  14
DEF  24

What I want to do is for every unique entry in column 1 average the values in column 2

Thanks,

Diya
# 2  
Old 11-16-2011
Code:
nawk '{c[$1]++;s[$1]+=$2}END{ for(i in c) printf("%s %.2f\n", i, s[i]/c[i])}' myFile

# 3  
Old 11-16-2011
Code:
awk '{ T[$1]+=$2; C[$1]++} END { for(K in C) print K, T[K]/C[K] }' < file

# 4  
Old 11-16-2011
Thanks .. It worked
# 5  
Old 11-17-2011
Code:
$
$
$ cat f21
ABC  6
ABC 22
ABC 44
ABC 56
XYZ 12
XYZ 23
XYZ 42
DEF  2
DEF  14
DEF  24
$
$
$ perl -lane 'if ($F[0] ne $i and $.>1){print $i,"\t",($j/$n); $j=$F[1]; $n=1} else{$n++; $j+=$F[1]} $i=$F[0]; END{print $i,"\t",($j/$n)}' f21
ABC     32
XYZ     25.6666666666667
DEF     13.3333333333333
$
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Match and rank question

I actually posted a similar question before but my expression probably lead the helper here frustrated. I also tried to figure it out by myself but failed. So I rephrase my question again and hopefully it is clearer to understand. I really appreciate your help. Here is the file1target:... (2 Replies)
Discussion started by: yuejian
2 Replies

2. UNIX for Dummies Questions & Answers

Partially match and rank question

Hi I am processing some files but have difficulty to deal with this one, hope someone can help me,Thank you in advance. I have a file 1 like this file 1:aa|M30369.1| mfe: -15.1 kc b|BX930090.1| mfe: -10.2 kc GOOD mfe: -20.3 kc BAD3.2 mfe: -25.4 kc I also have a file 2 like this file2:GOOD... (8 Replies)
Discussion started by: yuejian
8 Replies

3. UNIX for Dummies Questions & Answers

Assigning rank to rows of numbers with directionality

I have a tab-delimited text file where the second column contains in increasing order and includes negative and positive numbers. I want to assign ranks to the rows based on the value of the second column. The tricky part is that the row where the second column is "0", will be assigned "0", the... (3 Replies)
Discussion started by: evelibertine
3 Replies

4. Shell Programming and Scripting

rank values -awk

Is it possible to rank the files based on second column? input a_3f 10 a_4r 8 a_5 8 b_y 6 output 1 a_3f 10 2 a_4r 8 2 a_5 8 3 b_y 6 (3 Replies)
Discussion started by: quincyjones
3 Replies

5. Web Development

Fix For Google Page Rank: Wordpress List Rank Dashboard Widget

Here is the fix for the recent Google changes to their pagerank API. For example, in the List Rank Dashboard Widget Wordpress Plugin (Version 1.7), in this plugin file: list-rank-dashboard-widget/wp-list-rank-class.php in this function: function getGooglePR($url) Change this line: ... (0 Replies)
Discussion started by: Neo
0 Replies

6. Shell Programming and Scripting

Rank based on column in Unix

Hello guys I have requirement to rank a flat file based on column separated by delimeter I/P 1783747|091411|1000 1783747|091411|2000 1783747|091411|2000 1783747|091411|2000 1783747|091411|3000 1783747|091411|4000 1783747|091411|5000 1783747|091411|6000 1783747|091411|7000... (6 Replies)
Discussion started by: Pratik4891
6 Replies
Login or Register to Ask a Question