How to replace Ip address in .xml file through shell script?

I have one .xml file. which contains the following line.

<ParamString StringId="PortAddress" StringValue="172.27.166.170" />
<ParamString StringId="PortAddress" StringValue="172.27.166.171" />
<ParamString StringId="PortAddress" StringValue="172.27.166.202" />
<ParamString StringId="PortAddress" StringValue="172.27.166.203" />
<ParamString StringId="PortAddress" StringValue="172.27.167.170" />
<ParamString StringId="PortAddress" StringValue="172.27.167.171" />
<ParamString StringId="PortAddress" StringValue="172.27.167.202" />
<ParamString StringId="PortAddress" StringValue="172.27.167.203" />

I want to replace IP address to 172.27.166.178 from 172.27.166.178 ,
172.27.166.171 to 172.27.166.179 and so on.

I want to make it through shell script.
How to write a shell script in this regard.

Where's the difference?

---------- Post updated at 15:35 ---------- Previous update was at 15:33 ----------

If that's a typo, and you want to add 8 to the last byte, where to start and where to stop? All addresses?

---------- Post updated at 15:36 ---------- Previous update was at 15:35 ----------

What if you get close to /above 246?

It is a typical error. It should be 172.27.166.170 in place of 172.27.166.178 i.e. replacing IP address from 172.27.166.170 to 172.27.166.178 , 172.27.166.171 to 172.27.166.179 and so on.

I think we have a language barrier here. When you say "It should be 172.27.166.170 in place of 172.27.166.178", we interpret that to mean that 172.27.166.178 should be changed to 172.27.166.170 and we are confused because the string 172.27.166.178 does not appear anywhere in your sample input.

  1. Please show us the exact output you are trying to produce from your sample input (using CODE tags).
  2. Answer RudiC's question about what should happen if the replacement values overflow a valid IP address segment.
  3. And, explain whether the idea is just to increment values or whether the code is supposed to detect when the same IP address appears in the input and produce the same output for all occurrences of that that input IP address in the output.

Let me clarify the think.

I want
172.27.166.178 in place of 172.27.166.170, 172.27.166.179 in place of 172.27.166.171,
172.27.166.226 in place of 172.27.166.202, 172.27.166.227 in place of 172.27.166.203 and so on.

It is now clear as mud. You read an IP address from the input and your write an IP address to the output. Sometimes you add 8 to the input address to get the desired output address; sometime you add 24. How are we supposed to know what value to add (or subtract) to (or from) which input values if you don't give us a clear description of how your code is supposed to determine what random number to add or subtract from the input address?

See my input from file is

<ParamString StringId="PortAddress" StringValue="172.27.166.170" /> <ParamString StringId="PortAddress" StringValue="172.27.166.171" /> <ParamString StringId="PortAddress" StringValue="172.27.166.202" /> <ParamString StringId="PortAddress" StringValue="172.27.166.203" /> <ParamString StringId="PortAddress" StringValue="172.27.167.170" /> <ParamString StringId="PortAddress" StringValue="172.27.167.171" /> <ParamString StringId="PortAddress" StringValue="172.27.167.202" /> <ParamString StringId="PortAddress" StringValue="172.27.167.203" />

My out put in the same .xml file will be,

<ParamString StringId="PortAddress" StringValue="172.27.166.178" /> <ParamString StringId="PortAddress" StringValue="172.27.166.179" /> <ParamString StringId="PortAddress" StringValue="172.27.166.226" /> <ParamString StringId="PortAddress" StringValue="172.27.166.227" /> <ParamString StringId="PortAddress" StringValue="172.27.167.178" /> <ParamString StringId="PortAddress" StringValue="172.27.167.179" /> <ParamString StringId="PortAddress" StringValue="172.27.167.226" /> <ParamString StringId="PortAddress" StringValue="172.27.167.227" />

Is that clear now?

Yes. You have an xml file. In that xml file you want to change four specific IP addresses to four other specific IP addresses. There is no pattern to the changes to be made to those IP addresses.

So, the commands:

sed 's/"172.27.166.170"/"172.27.166.178"/g
s/"172.27.166.171"/"172.27.166.179"/g
s/"172.27.166.202"/"172.27.166.226"/g
s/"172.27.166.203"/"172.27.166.227"/g' file.xml > file.xml.$$ && cp file.xml.$$ file.xml
rm -f file.xml.$$
1 Like

If I want to store the ip address in a variable e.g. for the below line
<ParamString StringId="PortAddress" StringValue="172.27.166.170" />I want to store .166.170 in a variable and replace that variable with value 166.178 and replace StringValue to "172.27.166.178" from 172.27.166.170 in the same file and so on.How can I do this?

Adapting DonC's code:

sed 's/"172.27'"${oldValue}"'"/"172.27'"${newValue}"'"/g file.xml > file.xml.$$ && cp file.xml.$$ file.xml
rm -f file.xml.$$

Note that XML allows for either single or double quotes, so:

sed 's/["\']172.27'"${oldValue}"'["\']/"172.27'"${newValue}"'"/g file.xml > file.xml.$$ && cp file.xml.$$ file.xml

This will change the singlequotes to doublequotes, but that is acceptable for attribute values.