How to compare two strings in dot separated version format in Bash?

前端 未结 29 1631
慢半拍i
慢半拍i 2020-11-22 06:52

Is there any way to compare such strings on bash, e.g.: 2.4.5 and 2.8 and 2.4.5.1?

29条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 07:12

    Well if you know the number of fields you can use -k n,n and get a super-simple solution

    echo '2.4.5
    2.8
    2.4.5.1
    2.10.2' | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 -g
    
    2.4.5
    2.4.5.1
    2.8
    2.10.2
    

提交回复
热议问题