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? |
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 |
Try word boundaries:
Code:
sed 's/\b192.168.9.10\b/10.0.0.11/g' file Code:
sed 's/\<192.168.9.10\>/10.0.0.11/g' file |
Ruby(1.9+)
Code:
$ ruby -ne 'puts $_.gsub(/192.168.9.10\b/,"10.10.10.10")' file |
Quote:
# 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 |
Which version of sed are you using? On which operating system?
|
Adding to this I am working on AIX machine .
|
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. |