Reputation: 681
I have to sort cell names like this (no matters if it comes from a file or a variable): expected result
BAR.A1
BAR.A1-1
BAR.A2
BAR.A3
BAR.A10
FOO.A1
FOO.B1
FOO.B1-1
What I tried results in this wrong order :
BAR.A1
BAR.B1
BAR.B1-1
FOO.A1
FOO.A10 <--- wrong place
FOO.A1-1
FOO.A2
FOO.A3
I tried many combinaisons of
sort -n
sort -h
sort -d
none works.
Any clue ?
Upvotes: 0
Views: 156
Reputation: 385655
perl -MSort::Key::Natural=natsort -e'print for natsort <>'
See Specifying file to process to Perl one-liner for usage.
Upvotes: 1
Reputation: 67467
version sort
$ sort -V file
BAR.A1
BAR.B1
BAR.B1-1
FOO.A1
FOO.A2
FOO.A3
FOO.A10
FOO.A1-1
updated based on clarified question
$ sort -t- -k1,1V -k2 file
BAR.A1
BAR.B1
BAR.B1-1
FOO.A1
FOO.A1-1
FOO.A2
FOO.A3
FOO.A10
Upvotes: 0
Reputation: 246807
It looks like you want to sort by field 1 alphabetically, then field 2 version-wise
sort -t. -k 1,1 -k2,2V <<END
FOO.A1
FOO.A1-1
FOO.A2
FOO.A3
FOO.A10
BAR.A1
BAR.B1
BAR.B1-1
END
BAR.A1
BAR.B1
BAR.B1-1
FOO.A1
FOO.A1-1
FOO.A2
FOO.A3
FOO.A10
Upvotes: 5
Reputation: 12515
There are still various unclear points (you are not saying exactly the output you want) but sort -g
should do it:
$ cat s.txt
FOO.A1
FOO.A1-1
FOO.A2
FOO.A3
FOO.A10
BAR.A1
BAR.B1
BAR.B1-1
$ sort -g s.txt
BAR.A1
BAR.B1
BAR.B1-1
FOO.A1
FOO.A1-1
FOO.A10
FOO.A2
FOO.A3
-g
is defined as such:
-g, --general-numeric-sort, --sort=general-numeric Sort by general numerical value. As opposed to -n, this option handles general floating points. It has a more permissive format than that allowed by -n but it has a significant performance drawback.
But -n
would fit here as well, so your desired output is not clear.
Based on your further updates, maybe this is what you want:
$ sort -t '.' -k 2 -V < s.txt
BAR.A1
FOO.A1
FOO.A1-1
FOO.A2
FOO.A3
FOO.A10
BAR.B1
BAR.B1-1
Upvotes: 1