Use GREP to replace a body of text

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








up vote
0
down vote

favorite












What is best way to use GREP to replace text from in through </div> with
walked down the path



The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>









share|improve this question























  • grep doesn't modify text.
    – muru
    Jan 28 at 6:16










  • You can use the command 'sed' for that.
    – NickT
    Jan 28 at 7:27














up vote
0
down vote

favorite












What is best way to use GREP to replace text from in through </div> with
walked down the path



The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>









share|improve this question























  • grep doesn't modify text.
    – muru
    Jan 28 at 6:16










  • You can use the command 'sed' for that.
    – NickT
    Jan 28 at 7:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











What is best way to use GREP to replace text from in through </div> with
walked down the path



The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>









share|improve this question















What is best way to use GREP to replace text from in through </div> with
walked down the path



The cat in the hat
jdlsknklnvlsdknld ======> The cat walked down the path
jkgsabkjkjksjakh
</div>






command-line text-processing grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 28 at 4:44









muru

131k19275473




131k19275473










asked Jan 28 at 4:08









David L. Rodgers

1




1











  • grep doesn't modify text.
    – muru
    Jan 28 at 6:16










  • You can use the command 'sed' for that.
    – NickT
    Jan 28 at 7:27
















  • grep doesn't modify text.
    – muru
    Jan 28 at 6:16










  • You can use the command 'sed' for that.
    – NickT
    Jan 28 at 7:27















grep doesn't modify text.
– muru
Jan 28 at 6:16




grep doesn't modify text.
– muru
Jan 28 at 6:16












You can use the command 'sed' for that.
– NickT
Jan 28 at 7:27




You can use the command 'sed' for that.
– NickT
Jan 28 at 7:27










2 Answers
2






active

oldest

votes

















up vote
3
down vote













Matching multiple lines can be fairly easily done using Perl.



perl -p0e 's# in .*</div># walked down the path.#sm'


Briefly put,




  • -p0e turn Perl into a sed-like streaming multi-line processor.

  • The s#...#...# portion signifies that you want to replace all characters from in to </div> (the first clause) with walked down the path (the second clause).


  • sm roughly means "treat newline symbols as all other characters.

This answer assumes that the input text has only one such pattern.



Relevant documentation pages can be found here and here on Perl's official website.






share|improve this answer





























    up vote
    2
    down vote













    Use grep to replace text? Good luck with that.



    sed will do it for you:



    sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file


    Notes




    • s/old/new/ replace old with new


    • ; separates commands


    • /begin/,/end/ operate within a range from the line with /begin/ to the line with /end/


    • grouping, for commands to use on the range


    • /The cat/n exclude the line with /The cat/ (do nothing and read the next line)


    • d delete (the rest of the lines in the range)





    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1000576%2fuse-grep-to-replace-a-body-of-text%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Matching multiple lines can be fairly easily done using Perl.



      perl -p0e 's# in .*</div># walked down the path.#sm'


      Briefly put,




      • -p0e turn Perl into a sed-like streaming multi-line processor.

      • The s#...#...# portion signifies that you want to replace all characters from in to </div> (the first clause) with walked down the path (the second clause).


      • sm roughly means "treat newline symbols as all other characters.

      This answer assumes that the input text has only one such pattern.



      Relevant documentation pages can be found here and here on Perl's official website.






      share|improve this answer


























        up vote
        3
        down vote













        Matching multiple lines can be fairly easily done using Perl.



        perl -p0e 's# in .*</div># walked down the path.#sm'


        Briefly put,




        • -p0e turn Perl into a sed-like streaming multi-line processor.

        • The s#...#...# portion signifies that you want to replace all characters from in to </div> (the first clause) with walked down the path (the second clause).


        • sm roughly means "treat newline symbols as all other characters.

        This answer assumes that the input text has only one such pattern.



        Relevant documentation pages can be found here and here on Perl's official website.






        share|improve this answer
























          up vote
          3
          down vote










          up vote
          3
          down vote









          Matching multiple lines can be fairly easily done using Perl.



          perl -p0e 's# in .*</div># walked down the path.#sm'


          Briefly put,




          • -p0e turn Perl into a sed-like streaming multi-line processor.

          • The s#...#...# portion signifies that you want to replace all characters from in to </div> (the first clause) with walked down the path (the second clause).


          • sm roughly means "treat newline symbols as all other characters.

          This answer assumes that the input text has only one such pattern.



          Relevant documentation pages can be found here and here on Perl's official website.






          share|improve this answer














          Matching multiple lines can be fairly easily done using Perl.



          perl -p0e 's# in .*</div># walked down the path.#sm'


          Briefly put,




          • -p0e turn Perl into a sed-like streaming multi-line processor.

          • The s#...#...# portion signifies that you want to replace all characters from in to </div> (the first clause) with walked down the path (the second clause).


          • sm roughly means "treat newline symbols as all other characters.

          This answer assumes that the input text has only one such pattern.



          Relevant documentation pages can be found here and here on Perl's official website.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 28 at 16:01

























          answered Jan 28 at 15:55









          undercat

          1209




          1209






















              up vote
              2
              down vote













              Use grep to replace text? Good luck with that.



              sed will do it for you:



              sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file


              Notes




              • s/old/new/ replace old with new


              • ; separates commands


              • /begin/,/end/ operate within a range from the line with /begin/ to the line with /end/


              • grouping, for commands to use on the range


              • /The cat/n exclude the line with /The cat/ (do nothing and read the next line)


              • d delete (the rest of the lines in the range)





              share|improve this answer
























                up vote
                2
                down vote













                Use grep to replace text? Good luck with that.



                sed will do it for you:



                sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file


                Notes




                • s/old/new/ replace old with new


                • ; separates commands


                • /begin/,/end/ operate within a range from the line with /begin/ to the line with /end/


                • grouping, for commands to use on the range


                • /The cat/n exclude the line with /The cat/ (do nothing and read the next line)


                • d delete (the rest of the lines in the range)





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Use grep to replace text? Good luck with that.



                  sed will do it for you:



                  sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file


                  Notes




                  • s/old/new/ replace old with new


                  • ; separates commands


                  • /begin/,/end/ operate within a range from the line with /begin/ to the line with /end/


                  • grouping, for commands to use on the range


                  • /The cat/n exclude the line with /The cat/ (do nothing and read the next line)


                  • d delete (the rest of the lines in the range)





                  share|improve this answer












                  Use grep to replace text? Good luck with that.



                  sed will do it for you:



                  sed 's/in the hat/walked down the path/;/The cat/,/</div>/ /The cat/n;d' file


                  Notes




                  • s/old/new/ replace old with new


                  • ; separates commands


                  • /begin/,/end/ operate within a range from the line with /begin/ to the line with /end/


                  • grouping, for commands to use on the range


                  • /The cat/n exclude the line with /The cat/ (do nothing and read the next line)


                  • d delete (the rest of the lines in the range)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 28 at 7:41









                  Zanna

                  48.3k13120229




                  48.3k13120229



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1000576%2fuse-grep-to-replace-a-body-of-text%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491