Ask sed to ignore all special characters [duplicate]











up vote
1
down vote

favorite













This question already has an answer here:




  • What characters do I need to escape when using sed in a sh script?

    3 answers




I use the following code to delete a line :



sed -i "0,/$DELETE_THIS/{/$DELETE_THIS/d;}" file.txt


But this code fails if the variable DELETE_THIS contains special characters, like ., /, * and so on...



Is there a way to tell sed to ignore all special characters and use them as basic text ?










share|improve this question















marked as duplicate by Jeff Schaller, don_crissti, thrig, roaima, Stephen Harris Dec 5 at 23:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    You would have to escape any special characters in your query expression. Related: What characters do I need to escape when using sed in a sh script?
    – Kusalananda
    Dec 5 at 11:49

















up vote
1
down vote

favorite













This question already has an answer here:




  • What characters do I need to escape when using sed in a sh script?

    3 answers




I use the following code to delete a line :



sed -i "0,/$DELETE_THIS/{/$DELETE_THIS/d;}" file.txt


But this code fails if the variable DELETE_THIS contains special characters, like ., /, * and so on...



Is there a way to tell sed to ignore all special characters and use them as basic text ?










share|improve this question















marked as duplicate by Jeff Schaller, don_crissti, thrig, roaima, Stephen Harris Dec 5 at 23:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    You would have to escape any special characters in your query expression. Related: What characters do I need to escape when using sed in a sh script?
    – Kusalananda
    Dec 5 at 11:49















up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:




  • What characters do I need to escape when using sed in a sh script?

    3 answers




I use the following code to delete a line :



sed -i "0,/$DELETE_THIS/{/$DELETE_THIS/d;}" file.txt


But this code fails if the variable DELETE_THIS contains special characters, like ., /, * and so on...



Is there a way to tell sed to ignore all special characters and use them as basic text ?










share|improve this question
















This question already has an answer here:




  • What characters do I need to escape when using sed in a sh script?

    3 answers




I use the following code to delete a line :



sed -i "0,/$DELETE_THIS/{/$DELETE_THIS/d;}" file.txt


But this code fails if the variable DELETE_THIS contains special characters, like ., /, * and so on...



Is there a way to tell sed to ignore all special characters and use them as basic text ?





This question already has an answer here:




  • What characters do I need to escape when using sed in a sh script?

    3 answers








sed regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 at 12:02









Jeff Schaller

37.9k1053123




37.9k1053123










asked Dec 5 at 11:38









bob dylan

4821615




4821615




marked as duplicate by Jeff Schaller, don_crissti, thrig, roaima, Stephen Harris Dec 5 at 23:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jeff Schaller, don_crissti, thrig, roaima, Stephen Harris Dec 5 at 23:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    You would have to escape any special characters in your query expression. Related: What characters do I need to escape when using sed in a sh script?
    – Kusalananda
    Dec 5 at 11:49
















  • 1




    You would have to escape any special characters in your query expression. Related: What characters do I need to escape when using sed in a sh script?
    – Kusalananda
    Dec 5 at 11:49










1




1




You would have to escape any special characters in your query expression. Related: What characters do I need to escape when using sed in a sh script?
– Kusalananda
Dec 5 at 11:49






You would have to escape any special characters in your query expression. Related: What characters do I need to escape when using sed in a sh script?
– Kusalananda
Dec 5 at 11:49












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted











  • You have a string in $DELETE_THIS, which you want to pass to sed in such a way that sed will treat it literally.


  • For this, you must quote all characters which are meaningful to sed.



  • Put a backslash before them. For example, using Bash syntax:



    DELETE_THIS="$(<<< "$DELETE_THIS" sed -e 's`[\/.*^$]`\&`g')"


  • This will convert, for example, ^ab[c-d]ef$ into ^ab[c-d]\ef$.







share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted











    • You have a string in $DELETE_THIS, which you want to pass to sed in such a way that sed will treat it literally.


    • For this, you must quote all characters which are meaningful to sed.



    • Put a backslash before them. For example, using Bash syntax:



      DELETE_THIS="$(<<< "$DELETE_THIS" sed -e 's`[\/.*^$]`\&`g')"


    • This will convert, for example, ^ab[c-d]ef$ into ^ab[c-d]\ef$.







    share|improve this answer

























      up vote
      1
      down vote



      accepted











      • You have a string in $DELETE_THIS, which you want to pass to sed in such a way that sed will treat it literally.


      • For this, you must quote all characters which are meaningful to sed.



      • Put a backslash before them. For example, using Bash syntax:



        DELETE_THIS="$(<<< "$DELETE_THIS" sed -e 's`[\/.*^$]`\&`g')"


      • This will convert, for example, ^ab[c-d]ef$ into ^ab[c-d]\ef$.







      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted







        • You have a string in $DELETE_THIS, which you want to pass to sed in such a way that sed will treat it literally.


        • For this, you must quote all characters which are meaningful to sed.



        • Put a backslash before them. For example, using Bash syntax:



          DELETE_THIS="$(<<< "$DELETE_THIS" sed -e 's`[\/.*^$]`\&`g')"


        • This will convert, for example, ^ab[c-d]ef$ into ^ab[c-d]\ef$.







        share|improve this answer













        • You have a string in $DELETE_THIS, which you want to pass to sed in such a way that sed will treat it literally.


        • For this, you must quote all characters which are meaningful to sed.



        • Put a backslash before them. For example, using Bash syntax:



          DELETE_THIS="$(<<< "$DELETE_THIS" sed -e 's`[\/.*^$]`\&`g')"


        • This will convert, for example, ^ab[c-d]ef$ into ^ab[c-d]\ef$.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 5 at 11:51









        AlexP

        7,0391024




        7,0391024















            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre