Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add a new column with the correct order in awk taking into account that the same position may be repeated

I am trying to generate a new column with the new ranking position for a series of millionaires. The problem is that many of them have the same amount of money and when I generate a list with the order as follows: NR>1{$0=$0", "NR-1} 1, it doesn’t take me into account if there are more than one or two millionaires with the same amount of money.

What I want to do would be for example to order something like this:

Name;Company;Billions    
Bill Gate;Microsoft;76
Manolo Chocolatero;Churrerias;2
Eduardo Mendoza;ED S.A;3
Juan Palotes;Circos S.A;2

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Name;Company;Billions;Ranking    
Bill Gate;Microsoft;76;1
Manolo Chocolatero;Churrerias;3;2
Eduardo Mendoza;ED S.A;2;3
Juan Palotes;Circos S.A;2;3

Any idea how to do this with awk?

>Solution :

awk allows you to use variables, e.g. one for the rank, and one for the last value. With that, you can increase the rank only if the current value differs from the last one (assuming that you sort the input previously).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading