bash sorting numbers with decimals [duplicate]

随声附和 提交于 2019-12-11 16:59:20

问题


I have a file like so:

1.1
3.2
1.2
1.10

I would like to sort the file so that it looks like so:

1.1
1.2
1.10
3.2

In other words, 1.10 is bigger than 1.2

I tried:

sort -nk 1,1 file

But I keep getting this, which is not what I want

1.1
1.10
1.2
3.2

Thanks


回答1:


With GNU sort:

sort -t "." -n -k1,1 -k2,2 file

Output:

1.1
1.2
1.10
3.2



回答2:


You may use the -V option.

sort -V numbers

However this option is only in GNU Coreutils and could be absent from other implementation.

See https://stackoverflow.com/a/35386002/1107536



来源:https://stackoverflow.com/questions/46243957/bash-sorting-numbers-with-decimals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!