How can I add a backslash using sed? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
0
down vote

favorite













This question already has an answer here:



  • Search and replace a pattern containing backslashes

    1 answer



I want to add a backslash printed between Hello and World in command line using sed. It should show output Hello World.



What do I have to do?







share|improve this question














marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49


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




    that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like hello world
    – Zanna
    Apr 22 at 7:05






  • 1




    Related: Search and replace a pattern containing backslashes
    – Zanna
    Apr 22 at 7:06














up vote
0
down vote

favorite













This question already has an answer here:



  • Search and replace a pattern containing backslashes

    1 answer



I want to add a backslash printed between Hello and World in command line using sed. It should show output Hello World.



What do I have to do?







share|improve this question














marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49


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




    that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like hello world
    – Zanna
    Apr 22 at 7:05






  • 1




    Related: Search and replace a pattern containing backslashes
    – Zanna
    Apr 22 at 7:06












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Search and replace a pattern containing backslashes

    1 answer



I want to add a backslash printed between Hello and World in command line using sed. It should show output Hello World.



What do I have to do?







share|improve this question















This question already has an answer here:



  • Search and replace a pattern containing backslashes

    1 answer



I want to add a backslash printed between Hello and World in command line using sed. It should show output Hello World.



What do I have to do?





This question already has an answer here:



  • Search and replace a pattern containing backslashes

    1 answer









share|improve this question













share|improve this question




share|improve this question








edited Apr 24 at 12:08









anonymous2

3,14541746




3,14541746










asked Apr 22 at 6:42









Garry14

61




61




marked as duplicate by muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49


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 muru, karel, David Foerster, Eric Carvalho, Aaron Apr 24 at 14:49


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




    that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like hello world
    – Zanna
    Apr 22 at 7:05






  • 1




    Related: Search and replace a pattern containing backslashes
    – Zanna
    Apr 22 at 7:06












  • 1




    that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like hello world
    – Zanna
    Apr 22 at 7:05






  • 1




    Related: Search and replace a pattern containing backslashes
    – Zanna
    Apr 22 at 7:06







1




1




that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like hello world
– Zanna
Apr 22 at 7:05




that backslash is not very useful. If it's intended to escape the space when the string is used again for something, it needs to come before the space, like hello world
– Zanna
Apr 22 at 7:05




1




1




Related: Search and replace a pattern containing backslashes
– Zanna
Apr 22 at 7:06




Related: Search and replace a pattern containing backslashes
– Zanna
Apr 22 at 7:06










3 Answers
3






active

oldest

votes

















up vote
2
down vote













If you have a file named hw containing Hello World, the sed command would be:



sed 's/ / \ /' hw


This displays the wanted result on the screen. If you want to edit the file, add -i:



sed -i 's/ / \ /' hw


The command replaces the space by spacespace. You need two \ because is an escape character.






share|improve this answer


















  • 3




    Perl people call this leaning toothpick syndrome for obvious reasons.
    – PerlDuck
    Apr 22 at 9:15

















up vote
1
down vote













I used a text file containing the texts Hello World and with this sed command:



sed 's/Hello/Hello \/' helloworld.txt


And the output is:



Hello World


Note: This can be used too:



sed 's/World/\ World/' helloworld.txt


The sed command finds the Hello text and adds a to the front and that outputs the result seen. The \ escapes the so it's seen as a real (literal) not a special character.






share|improve this answer





























    up vote
    -3
    down vote













    I used at the end of code like this:



    sed -i -r "Hello \ World \" mytext.txt 


    This resulted in Hello World, as desired. But thank you, all.






    share|improve this answer






















    • Sorry I was wrong typing for my answer and I have edited it..
      – Garry14
      Apr 23 at 2:53






    • 3




      Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
      – Zanna
      Apr 23 at 5:50










    • Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
      – David Foerster
      Apr 24 at 8:32






    • 2




      @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
      – Eliah Kagan
      Apr 24 at 9:06







    • 3




      One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
      – Zanna
      Apr 24 at 9:12

















    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    If you have a file named hw containing Hello World, the sed command would be:



    sed 's/ / \ /' hw


    This displays the wanted result on the screen. If you want to edit the file, add -i:



    sed -i 's/ / \ /' hw


    The command replaces the space by spacespace. You need two \ because is an escape character.






    share|improve this answer


















    • 3




      Perl people call this leaning toothpick syndrome for obvious reasons.
      – PerlDuck
      Apr 22 at 9:15














    up vote
    2
    down vote













    If you have a file named hw containing Hello World, the sed command would be:



    sed 's/ / \ /' hw


    This displays the wanted result on the screen. If you want to edit the file, add -i:



    sed -i 's/ / \ /' hw


    The command replaces the space by spacespace. You need two \ because is an escape character.






    share|improve this answer


















    • 3




      Perl people call this leaning toothpick syndrome for obvious reasons.
      – PerlDuck
      Apr 22 at 9:15












    up vote
    2
    down vote










    up vote
    2
    down vote









    If you have a file named hw containing Hello World, the sed command would be:



    sed 's/ / \ /' hw


    This displays the wanted result on the screen. If you want to edit the file, add -i:



    sed -i 's/ / \ /' hw


    The command replaces the space by spacespace. You need two \ because is an escape character.






    share|improve this answer














    If you have a file named hw containing Hello World, the sed command would be:



    sed 's/ / \ /' hw


    This displays the wanted result on the screen. If you want to edit the file, add -i:



    sed -i 's/ / \ /' hw


    The command replaces the space by spacespace. You need two \ because is an escape character.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 24 at 8:31









    David Foerster

    26.1k1361106




    26.1k1361106










    answered Apr 22 at 6:52









    muclux

    2,1231521




    2,1231521







    • 3




      Perl people call this leaning toothpick syndrome for obvious reasons.
      – PerlDuck
      Apr 22 at 9:15












    • 3




      Perl people call this leaning toothpick syndrome for obvious reasons.
      – PerlDuck
      Apr 22 at 9:15







    3




    3




    Perl people call this leaning toothpick syndrome for obvious reasons.
    – PerlDuck
    Apr 22 at 9:15




    Perl people call this leaning toothpick syndrome for obvious reasons.
    – PerlDuck
    Apr 22 at 9:15












    up vote
    1
    down vote













    I used a text file containing the texts Hello World and with this sed command:



    sed 's/Hello/Hello \/' helloworld.txt


    And the output is:



    Hello World


    Note: This can be used too:



    sed 's/World/\ World/' helloworld.txt


    The sed command finds the Hello text and adds a to the front and that outputs the result seen. The \ escapes the so it's seen as a real (literal) not a special character.






    share|improve this answer


























      up vote
      1
      down vote













      I used a text file containing the texts Hello World and with this sed command:



      sed 's/Hello/Hello \/' helloworld.txt


      And the output is:



      Hello World


      Note: This can be used too:



      sed 's/World/\ World/' helloworld.txt


      The sed command finds the Hello text and adds a to the front and that outputs the result seen. The \ escapes the so it's seen as a real (literal) not a special character.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        I used a text file containing the texts Hello World and with this sed command:



        sed 's/Hello/Hello \/' helloworld.txt


        And the output is:



        Hello World


        Note: This can be used too:



        sed 's/World/\ World/' helloworld.txt


        The sed command finds the Hello text and adds a to the front and that outputs the result seen. The \ escapes the so it's seen as a real (literal) not a special character.






        share|improve this answer














        I used a text file containing the texts Hello World and with this sed command:



        sed 's/Hello/Hello \/' helloworld.txt


        And the output is:



        Hello World


        Note: This can be used too:



        sed 's/World/\ World/' helloworld.txt


        The sed command finds the Hello text and adds a to the front and that outputs the result seen. The \ escapes the so it's seen as a real (literal) not a special character.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 23 at 5:58









        Melebius

        3,75741636




        3,75741636










        answered Apr 22 at 6:53









        George Udosen

        17k93559




        17k93559




















            up vote
            -3
            down vote













            I used at the end of code like this:



            sed -i -r "Hello \ World \" mytext.txt 


            This resulted in Hello World, as desired. But thank you, all.






            share|improve this answer






















            • Sorry I was wrong typing for my answer and I have edited it..
              – Garry14
              Apr 23 at 2:53






            • 3




              Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
              – Zanna
              Apr 23 at 5:50










            • Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
              – David Foerster
              Apr 24 at 8:32






            • 2




              @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
              – Eliah Kagan
              Apr 24 at 9:06







            • 3




              One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
              – Zanna
              Apr 24 at 9:12














            up vote
            -3
            down vote













            I used at the end of code like this:



            sed -i -r "Hello \ World \" mytext.txt 


            This resulted in Hello World, as desired. But thank you, all.






            share|improve this answer






















            • Sorry I was wrong typing for my answer and I have edited it..
              – Garry14
              Apr 23 at 2:53






            • 3




              Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
              – Zanna
              Apr 23 at 5:50










            • Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
              – David Foerster
              Apr 24 at 8:32






            • 2




              @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
              – Eliah Kagan
              Apr 24 at 9:06







            • 3




              One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
              – Zanna
              Apr 24 at 9:12












            up vote
            -3
            down vote










            up vote
            -3
            down vote









            I used at the end of code like this:



            sed -i -r "Hello \ World \" mytext.txt 


            This resulted in Hello World, as desired. But thank you, all.






            share|improve this answer














            I used at the end of code like this:



            sed -i -r "Hello \ World \" mytext.txt 


            This resulted in Hello World, as desired. But thank you, all.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 24 at 8:36









            Eliah Kagan

            79.5k20221359




            79.5k20221359










            answered Apr 22 at 7:20









            Garry14

            61




            61











            • Sorry I was wrong typing for my answer and I have edited it..
              – Garry14
              Apr 23 at 2:53






            • 3




              Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
              – Zanna
              Apr 23 at 5:50










            • Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
              – David Foerster
              Apr 24 at 8:32






            • 2




              @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
              – Eliah Kagan
              Apr 24 at 9:06







            • 3




              One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
              – Zanna
              Apr 24 at 9:12
















            • Sorry I was wrong typing for my answer and I have edited it..
              – Garry14
              Apr 23 at 2:53






            • 3




              Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
              – Zanna
              Apr 23 at 5:50










            • Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
              – David Foerster
              Apr 24 at 8:32






            • 2




              @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
              – Eliah Kagan
              Apr 24 at 9:06







            • 3




              One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
              – Zanna
              Apr 24 at 9:12















            Sorry I was wrong typing for my answer and I have edited it..
            – Garry14
            Apr 23 at 2:53




            Sorry I was wrong typing for my answer and I have edited it..
            – Garry14
            Apr 23 at 2:53




            3




            3




            Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
            – Zanna
            Apr 23 at 5:50




            Thanks, that does make a little more sense, but it's still missing an actual sed command I think? Can you check again what exactly you wrote?
            – Zanna
            Apr 23 at 5:50












            Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
            – David Foerster
            Apr 24 at 8:32




            Is this supposed to be an answer or an explanation of what you tried (incorrectly) before you read the other answers?
            – David Foerster
            Apr 24 at 8:32




            2




            2




            @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
            – Eliah Kagan
            Apr 24 at 9:06





            @Garry14 The command you typed here was sed -i -r "Hello \ World \" mytext.txt, but each \ was displayed as . That's fixed now, and we've undeleted this, but it still doesn't fully make sense. You need a command, like c, to make this work. That is, sed -i -r "cHello \ World \" mytext.txt (better written sed -i -r 'cHello World ' mytext.txt) does work. Is it possible you ran such a command? Can you edit to clarify? Once c or something like it is added, the backslash at the end does actually make the one in the middle appear, which is quite interesting.
            – Eliah Kagan
            Apr 24 at 9:06





            3




            3




            One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
            – Zanna
            Apr 24 at 9:12




            One possibility is that rather than the -i flag, or additionally to the -i flag, you used sed's i (insert) command. That would produce the output you showed, provided there was already something in the file. Note that since the shell will not do anything to i by itself, it needn't be quoted. This works: sed i"Hello \ World \" and this also works sed "iHello \ World \" (whether or not the -i and -r flags are included)
            – Zanna
            Apr 24 at 9:12


            Popular posts from this blog

            pylint3 and pip3 broken

            Missing snmpget and snmpwalk

            How to enroll fingerprints to Ubuntu 17.10 with VFS491