rank values -awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rank values -awk
# 1  
Old 10-31-2011
rank values -awk

Is it possible to rank the files based on second column?

input

Code:
a_3f  10
a_4r  8
a_5  8
b_y  6

output
Code:
1  a_3f  10
2  a_4r  8
2  a_5  8
3  b_y  6

# 2  
Old 11-01-2011
Try this...
Code:
awk ' {
  if(val!=$2){ rank++; }
  printf("%s\t%s\t%s\n",rank,$1,$2)
  val=$2
} ' <(sort inputfile)

--ahamed

Last edited by ahamed101; 11-01-2011 at 12:25 AM..
# 3  
Old 11-01-2011
Some thing wrong.

My second column may have negative values. Is it the reason why your code is not working properly ?
# 4  
Old 11-01-2011
Post the inputfile...

--ahamed

---------- Post updated at 08:48 PM ---------- Previous update was at 08:46 PM ----------

Code:
awk ' {
  if(val!=$2){ rank++; }
  printf("%s\t%s\t%s\n",rank,$1,$2)
  val=$2
} ' <(sort -k2 -nr inputfile)

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 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. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

5. Shell Programming and Scripting

average rank order

Hi, I have a file with 2 columns 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 (4 Replies)
Discussion started by: Diya123
4 Replies

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

7. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question