Assigning rank to rows of numbers with directionality


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Assigning rank to rows of numbers with directionality
# 1  
Old 08-02-2012
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 rows with negative values in the second column ranks of increasingly negative values and the rows with positive values in the second column ranks of increasingly positive values. If two rows have the same value in their second column, they will have the same rank. Example:

Input:
Code:
A -5
B -4
C -4 
D -2
E 0
F 1
G 1
H 2

Code:
Output: 
A -5 -3
B -4 -2
C -4 -2
D -2 -1
E 0 0
F 1 1 
G 1 1 
H 2 2

How do I go about doing this? Thanks!
# 2  
Old 08-02-2012
how do you determine the 'rank'?
# 3  
Old 08-02-2012
Basically it is based on the value of the second column. The greater the value in the second column, the higher the rank. If two rows have the same value though, they have the same rank. The rank of the row with "0" in the second column is set to 0.
# 4  
Old 08-02-2012
Try:
Code:
awk 'NR==FNR&&$2<=0&&!a[$2]++{i--}NR!=FNR{$3=$2==tmp?i:++i;print;tmp=$2}' file file

Note that filename is specified twice.

Last edited by bartus11; 08-02-2012 at 04:34 PM.. Reason: Slight modification
This User Gave Thanks to bartus11 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. UNIX for Dummies Questions & Answers

Assigning rank to rows of numbers based on the last column

I have a tab delimited text file that looks like the following: ERBB3 0.00097 IL31RA 0.000972 SETD5 0.000972 MCART1 0.000973 CENPJ 0.000973 FNDC6 0.000974 I want to assign a number to each row based on the value in the last column (in the order of increasing value so that the first row... (3 Replies)
Discussion started by: evelibertine
3 Replies

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

4. Shell Programming and Scripting

Insert rows based on line numbers

Can I insert rows based on line numbers. Say If I need to insert 1 or more rows in a file from line number 10. Can I do that in UNIX I have a file something like A B C D E F After row C, I wanted to add 2 records as X and Y. I have the line number after C as my reference. Can I... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

5. Shell Programming and Scripting

Code for giving the frequency of numbers appearing in rows

I have a data file that has 6 columns and I have sorted the 3rd column so that all rows are sorted according to the 3rd column (from lowest to highest number). An example is shown below: 7563 273 1 -15.81100000 25.37250000 -19.27320000 8149 294 1 -17.90540000 ... (3 Replies)
Discussion started by: ananyob
3 Replies

6. Shell Programming and Scripting

Delete rows based on line numbers in a file

I have to find the number of rows in a file and delete those many rows in another file. For example, if I have 3 rows in a file A, i have to delete first 3 rows in anothe file B, I have the code, it works as standalone, when I merge this with m application (c with unix), it doesnt work. ... (2 Replies)
Discussion started by: Muthuraj K
2 Replies

7. Shell Programming and Scripting

count numbers of matching rows and replace its value in another file

Hello all, can you help me in this problem, assume We have two txt file (file_1 and file_3) one is file_1 contains the data: a 0 b 1 c 3 a 7 b 4 c 5 b 8 d 6 . . . . and I need to count the lines with the matching data (a,b,..) and print in new file called file_2 such as the... (4 Replies)
Discussion started by: GoldenFalcon10
4 Replies

8. Shell Programming and Scripting

Reversing numbers in a datafile of rows and columns

Hello, I've tried searching the forum for an answer to my question, but without any luck... I have a datafile looking simplified as follows: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 I want to reverse it by rearranging all the numbers from last to... (16 Replies)
Discussion started by: mattings
16 Replies

9. UNIX for Dummies Questions & Answers

Find different column numbers among rows in data

I want to find the different column numbers among rows in a file. For example: A001 a b c d e ... N A002 a b c d e ... N A003 a b c d e ... N+1 A004 a b c d e ... N A005 a b c d e ... N+2 : : For most of the lines I will have N columns (say 1000 rows) in each line except the line 3... (5 Replies)
Discussion started by: AMBER
5 Replies
Login or Register to Ask a Question