LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using sed to send a variable and replace (https://www.linuxquestions.org/questions/linux-newbie-8/using-sed-to-send-a-variable-and-replace-836574/)

greipr 10-06-2010 12:21 PM

using sed to send a variable and replace
 
Hi! I can't figure out the exact sed to make this work. I've installed CentOS and am trying to pass variables to a network-config file.

variables:
$ipAddress
$netmask
$gateway

file:
DeviceList.Ethernet.eth0.IP=192.168.1.10
DeviceList.Ethernet.eth0.Netmask=255.255.255.0
DeviceList.Ethernet.eth0.Gateway=192.168.1.1

I'm looking to replace the eth0.IP=*.*.*.* with eth0.IP=$ipAddress, and so on for netmask and gateway. How could I get this to work?

crts 10-06-2010 01:07 PM

Hi,
try this
Code:

sed -i -r "s/(eth0\.IP=)[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/\1$ipAddress/" filename
[EDIT]
A bit more elegant
Code:

sed -i -r "s/(eth0\.IP=)([0-9]{1,3}\.){3}[0-9]{1,3}/\1$ipAddress/" filename


All times are GMT -5. The time now is 06:19 AM.