Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed match exact

Tags:

linux

bash

sed

I'm currently using 's/string/new_string/g' to go through some CSV files, however I'm running into difficulty in the following scenario:

I want to replace instances of '192.168.0.11', however it's also catching instances of '192.168.0.111'.

How can I ensure sed only grabs '192.168.0.11' and not '192.168.0.111'? I'll obviously have to repeat this for many variables, so something easily scalable would be appreciated.

Thanks!

like image 832
Numpty Avatar asked Apr 24 '13 15:04

Numpty


People also ask

How do I change the exact word in Unix?

sed s/"sample_name ">/"sample_01 "/g So that it replaces only the desired word. For example the above syntax will replace word "the" from a text file and not from words like thereby.

How do I grep exact string in Linux?

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.


1 Answers

I'm not sure what the regex you are using is, but if you want an exact match of '192.168.0.11' you can use: s/\<192\.168\.0\.11\>//g

The \<\> force an exact match.

like image 128
dave Avatar answered Oct 19 '22 11:10

dave