Delete pattern from a variable with sed

£可爱£侵袭症+ 提交于 2021-02-09 12:25:20

问题


I am working with a script that has a variable called PRODUCT_VERSION. The version comes with a dot (for example 6.0). I need to remove the dot and save the result in another variable.

So far I come with this, but it does not work correctly

PRD_VER=$(sed "s/$PRODUCT_VERSION/\.//g")

回答1:


$ PRODUCT_VERSION=6.0

$ PRD_VER=${PRODUCT_VERSION/.}

$ echo $PRD_VER
60

Bash String Manipulation Examples




回答2:


This might work for you (GNU sed & bash):

NEW=$(sed 's/\.//g' <<<"$OLD")

or

NEW=$(echo "$OLD" | sed 's/\.//g')

However Mr Penny's answer is by far the best approach.



来源:https://stackoverflow.com/questions/16048316/delete-pattern-from-a-variable-with-sed

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