LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-26-2019, 03:05 PM   #1
langloisc
LQ Newbie
 
Registered: Feb 2019
Posts: 4

Rep: Reputation: Disabled
Perform replacement of ip address in file with variable


Hello all,

I am attempting to setup a script to change the IP address of a machine with the user specified one. I figured I could use sed to do this but so far I've been unsuccessful. Basically this is what I have:

The user sets the IP address at the beginning of the script which sets the variable.

I then want to only match IPADDR in /etc/sysconfig/network-scripts/ifcfg-eth0 and just replace the "3.3.3.3" (with the quotes) to whatever $IP is. So it would then be IPADDR="4.4.4.4" for example. I can successfully change it if I match the current IP address in the file, but I want it to work even if the IP address happens to change.

I figured you could do something like sed -i "s/^IPADDR=*.([0-9]+.[0-9]+.[0-9]+.[0-9]+)/$IP/g but that doesn't seem to be it. Any suggestions?
 
Old 02-26-2019, 03:05 PM   #2
langloisc
LQ Newbie
 
Registered: Feb 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
OK. I think I figured out my own question. I wasn't escaping the quotes.

The machines IP was 3.3.3.3.
I set the variable $IP with 4.4.4.4 in the script.

I performed sed -i -r "s/IPADDR=.*/IPADDR="$IP"/g" on the ifcfg-eth0 file, which then changed it to 4.4.4.4. Probably some unneeded syntax in there like /g, but I'm happy it works at least.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perform replacement of ip address in file with variable langloisc Linux - Newbie 3 02-26-2019 03:29 PM
How to perform a global character replacement in a txt, excluding some lines nouse Linux - Newbie 4 02-18-2014 10:10 AM
Sed: perform replacement only on even-numbered lines? kmkocot Linux - Newbie 3 07-20-2010 01:27 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Complicated VI replacement with variable expression jhwilliams Linux - Software 1 08-17-2007 09:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration