I've got a COTS product that requires we set a certain configuration property with a DNS name, rather than an IP address. Fortunately, our company standardized what the DNS names for that class of servers will look like. For example, an IP of 10.2.100.50
would have a DNS name of S010002100050.mycorp.com
. That is, it takes each octet from the IP address, prepends enough 0's that the value has exactly 3 characters, and then appends the DNS suffix. Simple, right?
My problem is, I need to take the IP address and convert it to the DNS name so I can apply it during my automated server config. I'm not the most skilled at sed, but I thought it could be used to do this. Unfortunately, I can't for the life of me figure out how. I came across a couple of related Stackoverflow questions (question 1, question 2) that talked about pre-pending 0's, but try as I might I couldn't get it to work as expected.
Can anyone give some advice and guidance on how I can accomplish this?? Thanks!!!
Would you please try:
ip="10.2.100.50"
sed -E 's/([0-9]+)/00\1/g; s/0*([0-9]{3})/\1/g; s/\.//g; s/(.*)/S\1.mycorp.com/' <<< "$ip"
Output:
S010002100050.mycorp.com
I had to divide the process into four steps:
s/([0-9]+)/00\1/g
prepends "00" to each octet.s/0*([0-9]{3})/\1/g
extracts the last three digits out of each octet.s/\.//g
removes the dots.s/(.*)/S\1.mycorp.com/
appends the suffix.Other language such as perl
may do it with fewer steps.