TKN
TKN

Reputation: 145

How to sort by a first field from lowest number to highest and than by a second field from highest to lowest number?

I have this data:

545:Jana Novakova:1995:Brno
6564:Jana Vikova:1984:Stara Paka
3207:Jiri Novak:1984:Nova Paka
893:Petra Maxova:2006:Praha

I need to sort it in the following order: From oldest person (third field) on the top to the youngest in the bottom. If they has the same year of birth (same number in the third field), than sort it by a first field from highest number in the top to lowest (just this part I do not know how to do).

I thought something like that would work, but it does not work: sort -t: -nk3 -nrk1

Upvotes: 2

Views: 49

Answers (1)

richyen
richyen

Reputation: 9978

[root@foo /]# cat foo 
545:Jana Novakova:1995:Brno
6564:Jana Vikova:1984:Stara Paka
3207:Jiri Novak:1984:Nova Paka
893:Petra Maxova:2006:Praha

[root@foo /]# sort -t: -nk3 -nk1r < foo 
6564:Jana Vikova:1984:Stara Paka
3207:Jiri Novak:1984:Nova Paka
545:Jana Novakova:1995:Brno
893:Petra Maxova:2006:Praha

Oldest persons on top 1984, then highest number in the top 6564 > 3207

Upvotes: 1

Related Questions