Home How can I give rank numbers to the sorted output?
Reply: 1

How can I give rank numbers to the sorted output?

Eileen
1#
Eileen Published in 2018-02-13 16:42:42Z

For example, if the input is:

Dog 100
Cat 200
Monkey 50
Elephant 20

I want the output to be:

1 Cat 200
2 Dog 100
3 Monkey 50
4 Elephant 20

I want to use the sort function to achieve this. Thanks!

Xavier Guihot
2#
Xavier Guihot Reply to 2018-02-13 16:47:04Z

In order to sort on the second element (-k2) of each line, and then to add the line nbr (| nl). The -r option is there to reverse order.

sort -k2 -rn file.txt | nl

which gives:

1   Cat 200
2   Dog 100
3   Monkey 50
4   Elephant 20
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.336383 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO