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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

735 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