Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace with sed when dots and underscores are present

How do I replace foo. with foo_ with sed simply running

sed 's/foo./foo_/g' file.php 

doesn't work. Thanks!

like image 741
algorithmicCoder Avatar asked May 25 '11 11:05

algorithmicCoder


People also ask

How do you replace special characters in sed?

You need to escape the special characters with a backslash \ in front of the special character. For your case, escape every special character with backslash \ .

How do you find and replace in sed?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace.

What does \b mean in sed?

\b marks a word boundary, either beginning or end. Now consider \b' . This matches a word boundary followed by a ' . Since ' is not a word character, this means that the end of word must precede the ' to match. To use \b to match at beginnings of words, reverse the order: '\b .


1 Answers

Escape the .:

sed 's/foo\./foo_/g' file.php 

Example:

~$ cat test.txt  foo.bar ~$ sed 's/foo\./foo_/g' test.txt  foo_bar 
like image 50
rid Avatar answered Oct 01 '22 02:10

rid