Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash sort for resolution

Tags:

bash

sorting

i have a list of resolutions (mobile devices, monitors etc...) and i want to sort them by resolution using bash/vim

i have (not ordered):

1024 x 480
96 x 120
1024 x 600
100 x 120
800 x 1280
64 x 98
640 x 320
64 x 128
640 x 960
800 x 480
80 x 120

then when i do:

cat file | sort -u

i get:

100 x 120
1024 x 480
1024 x 600
640 x 320
640 x 960
64 x 128
64 x 98
800 x 1280
800 x 480
80 x 120
96 x 120

why it not sorted like this and how can i do it?

64 x 98
64 x 128
80 x 120
96 x 120
100 x 120
640 x 320
640 x 960
800 x 1280
800 x 480
1024 x 480
1024 x 600
like image 993
aki Avatar asked Dec 28 '11 14:12

aki


1 Answers

you can use

sort -n -k1 -k3 file

This does the sort based on the numeric prefix so you'd get the list sorted by the first number

this produces

64 x 98
64 x 128
80 x 120
96 x 120
100 x 120
640 x 320
640 x 960
800 x 480
800 x 1280
1024 x 480
1024 x 600
like image 82
MahdeTo Avatar answered Oct 21 '22 21:10

MahdeTo