Sort numbers with multiple decimals in bash

In bashuse sortwith the option -ndoes not give the expected result.

$ cat numbers | sort -n
1.0
1.1
1.11.4
1.15
1.3
1.3.3
1.4-p1
1.6.1
2.2.10
2.2.2
2.4
2.4.6

I tried to use -k1, -k1.1netc. ( -k1.3ngets the order correct only for numbers starting with 1). It seems that there is something very simple, I'm not here ...

+6
source share
3 answers

You need -t. flag to indicate '.' both your separator and the position qualifiers with several keywords process gradually longer / deeper numbers. I still don't quite understand how this works, but it works ...

 sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n numbers

or

 cat numbers | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n
+6

-V

$ sort -V numbers

1.0
1.1
1.3
1.3.3
1.4-p1
1.6.1
1.11.4
1.15
2.2.2
2.2.10
2.4
2.4.6

. GNU Coreutils .

+7
sort -g numbers

It will do. According to the sorting man page, it is -gintended for numerical sorting:

-g, --general-numeric-sort

compare by total numerical value

0
source

Source: https://habr.com/ru/post/1628671/


All Articles