Split a string in shell to get all fields upto final '.' (Bash)

后端 未结 1 1474
孤独总比滥情好
孤独总比滥情好 2021-01-24 15:31

I\'m looking to get 1.2.3.4 of string 1.2.3.4.5 I know that I can use this code to get the final number, but what can I use to get all numbers previous. (Including \':\')

<
相关标签:
1条回答
  • 2021-01-24 15:43

    Try using:

    echo ${foo%.*}
    

    Yielding:

    foo="1.2.3.4.5"
    echo ${foo%.*}
    1.2.3.4
    

    and

    foo="1 2 6"
    echo ${foo%.*}
    

    yields

    1 2 6
    

    If you want to convert the spaces to ., you can use tr:

    echo $foo | tr ' ' '.'
    

    Yielding the following for foo="1 2 6"

    1.2.6
    
    0 讨论(0)
提交回复
热议问题