How to change ip using awk or sed .

How to change ip using awk or sed .
#cat /etc/hosts
10.151.5.1 server1
10.151.5.2 server2
10.151.5.3 server3
10.151.5.4 server4
10.151.5.5 server5

Output:
10.151.5.1 server1
10.181.5.2 server2
10.151.5.3 server3
10.181.5.4 server4
10.181.5.5 server5

why don't you just edit the file manually ?

ya, I would also suggest to do that manually, as your replace requirement is not consistent.

If you want all 151's in 2nd field to be 181, then

$ awk 'BEGIN{OFS=FS="."}$2==151{$2=181}{print}' hosts
10.181.5.1 server1
10.181.5.2 server2
10.181.5.3 server3
10.181.5.4 server4
10.181.5.5 server5

//Jadu

Its just an example, for instance you have lots of servers' you want to edit what's in your list.

Ok how can you edit just server3 only? Not all just one. Can you do that?

small modification on Jadu's awk command will do.

awk '/server3/ BEGIN{OFS=FS="."}$2==151{$2=181}{print}' hosts

this should be fine

it doesnt work .it will change all,

awk 'BEGIN{OFS=FS="."} /server3/ { if($2==151) {$2=181}} {print}' hosts

try this

you can use while statement, so that you will only change the one in your list.

To change only server3, then

sed -e "/server3/s/151/181/g" input.txt

#cat /etc/hosts
10.151.5.1 server1
10.151.5.2 server2
10.151.5.3 server3
10.151.5.4 server4
10.151.5.5 server5

Output:
10.151.5.1 server1
10.181.5.2 server2
10.151.5.3 server3
10.181.5.4 server4
10.181.5.5 server5

any script for this? i already create a list for that it will easily changed. any script?what loop will i use