# Assigning rank to rows of numbers with directionality

**URL:** <https://community.unix.com/t/assigning-rank-to-rows-of-numbers-with-directionality/314590>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [August 2, 2012, 3:05pm UTC](https://community.unix.com/t/assigning-rank-to-rows-of-numbers-with-directionality/314590 "2012-08-02T15:05:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evelibertine](https://community.unix.com/letter_avatar/evelibertine/32/5_5575768a8748004e209b776fc1b2916d.png) [@evelibertine](https://community.unix.com/u/evelibertine)\
**Post date:** [August 2, 2012, 3:05pm UTC](https://community.unix.com/t/assigning-rank-to-rows-of-numbers-with-directionality/314590/1 "2012-08-02T15:05:49Z")

</div>

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:

```nohighlight
A -5
B -4
C -4 
D -2
E 0
F 1
G 1
H 2

```

```nohighlight
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!

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [August 2, 2012, 3:09pm UTC](https://community.unix.com/t/assigning-rank-to-rows-of-numbers-with-directionality/314590/2 "2012-08-02T15:09:46Z")

</div>

how do you determine the 'rank'?

---

<div class="post-metadata">

**Author:** ![evelibertine](https://community.unix.com/letter_avatar/evelibertine/32/5_5575768a8748004e209b776fc1b2916d.png) [@evelibertine](https://community.unix.com/u/evelibertine)\
**Post date:** [August 2, 2012, 3:13pm UTC](https://community.unix.com/t/assigning-rank-to-rows-of-numbers-with-directionality/314590/3 "2012-08-02T15:13:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bartus11](https://community.unix.com/user_avatar/community.unix.com/bartus11/32/1418_2.png) [@bartus11](https://community.unix.com/u/bartus11)\
**Post date:** [August 2, 2012, 3:15pm UTC](https://community.unix.com/t/assigning-rank-to-rows-of-numbers-with-directionality/314590/4 "2012-08-02T15:15:46Z")

</div>

Try:

```nohighlight
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.
