LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-18-2012, 06:48 PM   #1
finekevin
LQ Newbie
 
Registered: Jan 2012
Posts: 6

Rep: Reputation: Disabled
Replace IP address string in a file using sed


Hi Guys,

I am new to linux and I need your help!

I need to replace an IP address in a file using "sed" command. The IP address could be any number (random) and the double quotes(") must be present as part of the string. There are numerous IP addresses in the file, but I only need to replace IP address within the following string only.

Current IP address string:
MY_REM_IPADDR="X.X.X.X"

Note: X represents random number.

New IP address string would be:
MY_REM_IPADDR="10.5.34.23"


I am using Fedora Linux with bash shell. It is an embedded system, so I have limited packages and I can't install additional packages. This is the reason, I need to use "sed" command.

Thank you,
Kevin
 
Old 01-18-2012, 07:33 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
To replace the IPv4 address in MY_REM_IPADDR="x.x.x.x" with something else, you could use this:

sed "s/\([0-9]\{1,3\}\.)\{3\}[0-9]\{1,3\}/new.ip.address/"

But that might replace other IP addresses as well. To match the exact string MY_REM_IPADDR="x.x.x.x" and replace the IPv4 address x.x.x.x with someting else, this should do the trick:

sed "s/^MY_REM_IPADDR=\"\([0-9]\{1,3\}\.)\{3\}[0-9]\{1,3\}/MY_REM_IPADDR=\"new.ip.address/"

The caret at the beginning matches the start of the line, so if MY_IP_ADDRESS could have preceding whitespace, it should be removed.

The regexp may seem unreadable, but that's mostly do to the excessive backslash escaping needed for bash. This it what's really going on:

[0-9] - Matches digits 0-9
[0-9]{1,3} - Matches from 1 to 3 digits (like in an IP address)
[0-9]{1,3}\. - Matches 1 to 3 digits followed by a dot (escape needed in regexes)
([0-9]{1,3}\.){3} - Exactly three sequences of 1 to 3 digits and a dot
([0-9]{1,3}\.){3}[0-9]{1,3} - ...followed another sequence, no dot this time

Now escape all parentheses and curly brackets, as well as the double quotes inside the regex, and it'll look like the above.

Last edited by Ser Olmy; 01-18-2012 at 07:39 PM. Reason: Replaced single with double quotes around the regex
 
Old 01-18-2012, 08:16 PM   #3
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
If you only want to replace the IP address on lines that contain the string MY_REM_IPADDR then limit the sed with that
Code:
sed '/MY_REM_IPADDR/s#[1-9.]*#10.5.34.23#'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem using sed to replace string in file umk Debian 12 02-01-2012 09:39 AM
[SOLVED] replace string in file using perl or sed wf201626 Programming 13 07-05-2011 03:29 AM
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 10 05-02-2011 03:30 PM
sed replace string xmickaelx Programming 3 04-07-2011 07:42 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration