Solved

Find / Replace escape

Posted on 2006-11-08
4
262 Views
Last Modified: 2008-02-01
Hello,

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
       do
          sed 's/\FIND/REPLACE/g' $fname > $fname.new
         mv -f $fname.new $fname
       done



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="link.name" 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?


Thanks!
0
Comment
Question by:hankknight
[X]
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
4 Comments
 
LVL 45

Accepted Solution

by:
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="link.name" 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

Cheers!
sunnycoder
0
 
LVL 16

Author Comment

by:hankknight
ID: 17898468
Thanks,

What about each of these?  Which ones need escaping?

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

Assisted Solution

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

Expert Comment

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

Cheers
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

621 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