How do I write this in BASH script? Escape Function for Sed

Posted on 2008-11-14
Last Modified: 2013-12-26

I am doing a lot of  find / -name "*" -type f -exec sed -i 's/STRING1/STRING2/' {} \;

But I am going crazy because everytime I need to manually escape chars like / and & in STRING1 and STRING2. Is there a way to write it or perhaps to use a extra function, so I dont have to escape strings like this - > Hosted for free by <a href="http:\/\/" target="_blank"><\/a> \&copy;  

Thank you.
Question by:Octalys
    1 Comment
    LVL 48

    Accepted Solution

    sed can use whatever character you like as a delimiter.  By default, most people use a forward slash as it is easier to read.

    So if you have

    sed -i 's#STRING1#STRING2#

    then you don't need to escape any forward slashes, and that will save you most of the extra typing.'

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
    The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now