echo $HOSTNAME
gives me ip-255-25-255-255
. I am trying to remove ip-
and replace -
with .
.
I can do:
a=${HOSTNAME#*-}
b=${a//-/.}
which gives me 255.25.255.255
.
Is there any way I can do this in one line?
echo $HOSTNAME
gives me ip-255-25-255-255
. I am trying to remove ip-
and replace -
with .
.
I can do:
a=${HOSTNAME#*-}
b=${a//-/.}
which gives me 255.25.255.255
.
Is there any way I can do this in one line?
Yes there is.
sed 's/^[^-]*-//;s/-/./g' <<< "$HOSTNAME"
yields the desired output.
s/^[^-]*-//
matches zero or more non-dash characters followed by a dash (see it online) and removes them,s/-/./g
replaces all dashes with dots.Using gsub
function of awk
:
echo 'ip-255-25-255-255' |awk '{gsub(/^[^-]+-/,"");gsub(/-/,".")}1'
255.25.255.255