Operating System - HP-UX
1827295 Members
3799 Online
109717 Solutions
New Discussion

sed/regular expression question

 
Grant Wenstrand_1
Occasional Contributor

sed/regular expression question

All,

I want to write a script that edits the ip address for my default gateway in my hosts file. I am using SED, but appear to have run into a bit of a problem--in that I can't determine how to properly quote the variable within the SED script properly. For Example:
-----------------------------------------------#HOSTS FILE
192.168.1.105 local
192.168.1.1 router

#VARIABLES
curgw=192.168.1.1
newgw=206.36.231.129

sed 's/'$curgw'/'$newgw' /etc/hosts > /var/tmp/hosts
-----------------------------------------------

My sed statement above replaces the current router ip address with the new router ip address, but it also turns my local ip address into "206.36.231.12905".

Would someone be so kind as to point out what I am doing wrong here?

Thank you,

Grant

7 REPLIES 7
Kofi ARTHIABAH
Honored Contributor

Re: sed/regular expression question

The reason is that local also has exactly the same prefix 192.168.1.1 - to make it unique, you can change your sed to:

sed 's/'$curgw' /'$newgw' /etc/hosts > /var/tmp/hosts

(ie. a space after curgw - that way it will not match 192.168.1.105 as well as 192.168.1.1 since there is a requirement for a space character.)

good luck
nothing wrong with me that a few lines of code cannot fix!
S.K. Chan
Honored Contributor

Re: sed/regular expression question

Need a space after '$curgw'

sed 's/'$curgw' /'$newgw' /etc/hosts> /var/tmp/hosts

Curtis Larson_1
Valued Contributor

Re: sed/regular expression question

try this:

sed 's/'$curgw'\([[:space:]]*\)/'$newgw'\1/' /etc/hosts > yourfile
Sridhar Bhaskarla
Honored Contributor

Re: sed/regular expression question

sed 's/'${curgw}' /'${newgw} /'

should do the trick for you.

Note the spaces after curgw and newgw.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Roger Baptiste
Honored Contributor

Re: sed/regular expression question

<>

Change this to
sed 's/'$curgw$/'$newgw'

(the $ at the end of curgw tells sed to match it exactly!.)

HTH
raj
Take it easy.
Grant Wenstrand_1
Occasional Contributor

Re: sed/regular expression question

Thank you all for your input. Regretablly, I am not yet successful, but I appear to be getting close using the POSIX character class suggestion of Curtis.

Cheers,

Grant
David W Lauderback
New Member

Re: sed/regular expression question

The others are correct about the space but a more general solution is to use a word end mark '\>' (i.e. your fields may be separated by tabs).
You should also be careful about '.'s in regular expresions. I would have used something like:

#VARIABLES
curgw='192\.168\.1\.1' # Note ' quotes \ where " sometimes quote newgw=206.36.231.129
sed "s/^$curgw\\>/$newgw/" /etc/hosts > /var/tmp/hosts

The ^ forces replace ONLY at beginning of line. Otherwise you might have the same problem as the space solves but at the beginning of the line. Say you are changing 10.1.1.1 to 10.1.2.1. The address 110.1.1.1 would be changed to 110.1.2.1.

These points are probably not interesting here but in a more complex case they may be.

Good Luck,
Dave L.