Find / Replace escape

Posted on 2006-11-08
Last Modified: 2008-02-01

I want to do a find and replace on ALL files in a directory, using this:

       find /home/me/directory/ -type f | while read fname
          sed 's/\FIND/REPLACE/g' $fname > $
         mv -f $ $fname

But I want to find this:
   <img src="images/my_image.jpg" width="150" height="50" hspace="5">

and replace it with this:
   <a href="" target="_blank"><img src="image.gif" width="50" height="50" hspace="5" border="0" /></a>

What charactors to I need to escape and how do I escape them?

Question by:hankknight
  • 2
LVL 45

Accepted Solution

sunnycoder earned 400 total points
ID: 17896994
Hi hankknight,

You need to escape each speacial character with a \  ... field separator is also special character in a sed command, so use : instead of /  and you dont have to escape the / characters ...

sed 's:<img src="images/my_image.jpg" width="150" height="50" hspace="5">:<a href="" target="_blank"><img src="image.gif" width="50" height="50" hspace="5" border="0" /></a>:g'

This should work ... Let me know the error if it fails

LVL 16

Author Comment

ID: 17898468

What about each of these?  Which ones need escaping?

? \ . [ ] ^ $ & % \ _ > + = | !
LVL 84

Assisted Solution

ozo earned 100 total points
ID: 17900265
\\  \. \[  \^  \$  
and perhaps \! depending on your shell
LVL 45

Expert Comment

ID: 17903716
Look at the man page of sed on your machine ... that should contain the list of special characters


Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux "time" command output redirection 16 118
nagios 1 22
nagios alerts 3 33
How can you tell your public IP address without a browser? 9 52
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now