how to sed for certain parts of a string by delimiter [closed]

江枫思渺然 提交于 2020-03-28 06:39:12

问题


I have lots of different strings that look like redhat-ubi-ubi7-7.8

I want to use the string to make variables so that I end up having something like

vendor=redhat
product=ubi
image=ubi7
tag=7.8

How can I do this?


回答1:


With bash and a here string:

string='redhat-ubi-ubi7-7.8'
IFS=- read -r vendor product image tag <<< "$string"
echo "$vendor"

Output:

redhat



回答2:


Using P.E. parameter expansion.

string='redhat-ubi-ubi7-7.8'
vendor=${string%%-*}
tag=${string##*-}
image=${string%-*}
product=${image#*-}
product=${product%-*}
image=${image##*-}

printf '%s\n' vendor=$vendor product=$product image=$image tag=$tag

Output

vendor=redhat
product=ubi
image=ubi7
tag=7.8


来源:https://stackoverflow.com/questions/60572406/how-to-sed-for-certain-parts-of-a-string-by-delimiter

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