LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 04-12-2011, 06:33 AM   #1
malkotisaab
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Rep: Reputation: 0
Unhappy sed find replace problem


# cat file
192.168.9.100
192.168.9.10 hi hello
# sed s/192.168.9.10/10.0.0.11/g file
10.0.0.110
10.0.0.11 hi hello

I want only 192.168.9.10 to be replaced. the command replacing the 192.168.9.100 as well.

Any suggestions?
 
Old 04-12-2011, 06:35 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Just extend your regex by adding a space...

sed s/192.168.9.10 /10.0.0.11/g file

Or if there's no "hi hello" generally, use $ to denote "end of line"

sed s/192.168.9.10$/10.0.0.11/g file

More:

http://www.regular-expressions.info/

man sed
 
Old 04-12-2011, 06:38 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Try word boundaries:
Code:
sed 's/\b192.168.9.10\b/10.0.0.11/g' file
or
Code:
sed 's/\<192.168.9.10\>/10.0.0.11/g' file
 
Old 04-12-2011, 07:19 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Ruby(1.9+)
Code:
$ ruby -ne 'puts $_.gsub(/192.168.9.10\b/,"10.10.10.10")' file
192.168.9.100
10.10.10.10 hi hello
 
Old 04-13-2011, 05:19 AM   #5
malkotisaab
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Try word boundaries:
Code:
sed 's/\b192.168.9.10\b/10.0.0.11/g' file
or
Code:
sed 's/\<192.168.9.10\>/10.0.0.11/g' file
Didnt work either:


# cat file1
192.168.9.100
192.168.9.10 hi hello
# sed 's/\b192.168.9.10\b/10.0.0.11/g' file1
192.168.9.100
192.168.9.10 hi hello
# sed 's/\<192.168.9.10\>/10.0.0.11/g' file1
192.168.9.100
192.168.9.10 hi hello

Last edited by malkotisaab; 04-13-2011 at 05:24 AM.
 
Old 04-13-2011, 05:31 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Which version of sed are you using? On which operating system?
 
Old 04-13-2011, 05:39 AM   #7
malkotisaab
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Adding to this I am working on AIX machine .
 
Old 04-14-2011, 02:20 AM   #8
malkotisaab
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Thakns for your inputs everybody.

Got the problem resovled with a little trick:

# cat file1
192.168.9.100
192.168.9.10 hi hello
# sed 's/192.168.9.10 /10.0.0.99 /g' file1
192.168.9.100
10.0.0.99 hi hello


Thanks
Yogesh
 
  


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
find or replace without using sed adarshmca Linux - Newbie 9 12-28-2010 05:41 AM
find or replace through sed adarshmca Linux - Newbie 3 12-27-2010 11:35 PM
problems using sed to find and replace.... tdnnash25 Linux - Server 4 03-01-2010 06:34 PM
Find several different words and replace with one using sed. Techno Guy Linux - Newbie 18 07-06-2009 07:16 AM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 07:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 10:12 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
Open Source Consulting | Domain Registration