Skip to content
Advertisement

Reverse sort 3rd numeric column in file

In bash shell scripting, I have users.txt file.

sort -t: -k 3n users.txt gives me


gg:g@g.g:
ff:f@f.f:5
gdr:d@sd.ds:5
g:w@f.com:8
r:w@d.com:85
qaws:aaa@d.com:789
qaz:a@s.com:789
vcf:d@c.com:855
op:popo@ppoo.po:6465
zx:a@c.com:7845
az:a@aa.com:7894
w:a@a.com:7894
h:g@g.com:7899
lp:l@l.com:8888
t:s@s.com:85474
dssdfsdf:fssdf@mbhkj.sdf:654654
u:f@g.comn:6546546
a:a@a.com:7894561
tt:t@t.t:1234567890

This is ok. But I need 3rd column which contain only phone number in reverse order i.e. descending order. How to achieve that?

I need like

tt:t@t.t:1234567890
a:a@a.com:7894561
u:f@g.comn:6546546
dssdfsdf:fssdf@mbhkj.sdf:654654
t:s@s.com:85474
lp:l@l.com:8888
and so on...

Advertisement

Answer

You could add an r (short for “reverse”) to the order column specification:

$ sort -t: -k 3nr  users.txt
# Here ---------^
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement