- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sed/regular expression question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 01:26 PM
12-06-2001 01:26 PM
sed/regular expression question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 01:35 PM
12-06-2001 01:35 PM
Re: sed/regular expression question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 01:36 PM
12-06-2001 01:36 PM
Re: sed/regular expression question
sed 's/'$curgw' /'$newgw' /etc/hosts> /var/tmp/hosts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 01:38 PM
12-06-2001 01:38 PM
Re: sed/regular expression question
sed 's/'$curgw'\([[:space:]]*\)/'$newgw'\1/' /etc/hosts > yourfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 01:40 PM
12-06-2001 01:40 PM
Re: sed/regular expression question
should do the trick for you.
Note the spaces after curgw and newgw.
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 01:42 PM
12-06-2001 01:42 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 03:21 PM
12-06-2001 03:21 PM
Re: sed/regular expression question
Cheers,
Grant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2001 09:55 PM
12-06-2001 09:55 PM
Re: sed/regular expression question
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.