What's Happening

the propper use of sort

Sep 7th, 2006 by blog | 0

well not rocket-science but handy to know: If you want to sort a file according to the first colum and then within the second colum you can use this command:

sort -k 1g -k 2g bla

g mean general numeric so that the 2 comes before 10.

view an example …

sorting a file like this:
10 10 97.0
10 6 77.21
10 8 88.12
20 10 170.64
20 6 143.7
20 8 158.9
2 10 26.99
2 6 16.99
2 8 22.55

first column first then the second, so that the result looks like this
2 6 16.99
2 8 22.55
2 10 26.99
10 6 77.21
10 8 88.12
10 10 97.0
20 6 143.7
20 8 158.9
20 10 170.64

using

sort -k 1g,2g bla

instead would mean to sort according to the first 2 chars in column 1

Leave a Reply