Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort according to second column numerically and first alphabetically

Tags:

bash

sorting

I have 2 columns, I want to sort them using bash.

I used the command:

sort -k2 -n
c 9
c 11
c 11
sh 11
c 13
c 15
txt 47
txt 94
txt 345
txt 628
sh 3673

This is the result, but i need them to be sorted like this:

c 9
c 11
c 11
c 13
c 15
sh 11
sh 3673
txt 47
txt 94
txt 345
txt 628

Any ideas?

like image 969
Holajz Avatar asked Mar 28 '16 14:03

Holajz


1 Answers

First sort by column 1, then by 2:

sort -k1,1 -k2,2n file.txt
like image 174
heemayl Avatar answered Sep 30 '22 01:09

heemayl