Extract version number from a string

前端 未结 5 2071
灰色年华
灰色年华 2020-11-29 12:40

I have a string with components and version numbers:

data-c(kuh-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);

5条回答
  •  星月不相逢
    2020-11-29 13:18

    sed can handle this easily....

    string="ata-c(kuh-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(kuh)-win2"
    
    echo $string | sed "s/^.*divider-bin-\([0-9.]*\).*/\1/"
    1.4.4
    

    There are a few other things you can do to tighten it up... such as stop grabbing the version number when you reach the ";"

    sed "s/^.*divider-bin-\([^;]*\).*/\1/"
    

提交回复
热议问题