问题
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