aki
aki

Reputation: 1241

bash sort for resolution

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

Upvotes: 8

Views: 377

Answers (3)

João Dantas
João Dantas

Reputation: 21

why not use something like (the important part here are the sort arguments):

xrandr -q | grep "   " | awk '{ print $1 }' | sort --unique --field-separator "x" -h

Upvotes: 0

MahdeTo
MahdeTo

Reputation: 11184

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

Upvotes: 16

gpojd
gpojd

Reputation: 23065

sort -n -k1 -k3  file
  • -n is to force numeric sorting
  • -k is telling it to sort using the first part then the third part of the string
  • file is the name of the file. There is no reason to use cat. The usage is:

    sort [OPTION]... [FILE]...

man sort

Upvotes: 1

Related Questions