LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   sed find replace problem (https://www.linuxquestions.org/questions/linux-server-73/sed-find-replace-problem-874543/)

malkotisaab 04-12-2011 06:33 AM

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?

AlucardZero 04-12-2011 06:35 AM

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

colucix 04-12-2011 06:38 AM

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

kurumi 04-12-2011 07:19 AM

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


malkotisaab 04-13-2011 05:19 AM

Quote:

Originally Posted by colucix (Post 4322086)
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

colucix 04-13-2011 05:31 AM

Which version of sed are you using? On which operating system?

malkotisaab 04-13-2011 05:39 AM

Adding to this I am working on AIX machine .

malkotisaab 04-14-2011 02:20 AM

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


All times are GMT -5. The time now is 09:30 PM.