Remove everything between specified characters including multi lines












2















I have a file with contents like so:



## this must go ##
## also
this
must go
##
hello world
##and this one
too##


I want to remove all between ##, including multi lines, so I am left just with hello world



This removes only part that is on one line:



sed -i.bak 's/##.*##//g' myfile


How to remove multi line stuff too?



P.S Im on MAC










share|improve this question

























  • Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).

    – Wiktor Stribiżew
    Nov 22 '18 at 14:26













  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!

    – bukowski
    Nov 22 '18 at 14:51











  • Posted with explanation.

    – Wiktor Stribiżew
    Nov 22 '18 at 14:55
















2















I have a file with contents like so:



## this must go ##
## also
this
must go
##
hello world
##and this one
too##


I want to remove all between ##, including multi lines, so I am left just with hello world



This removes only part that is on one line:



sed -i.bak 's/##.*##//g' myfile


How to remove multi line stuff too?



P.S Im on MAC










share|improve this question

























  • Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).

    – Wiktor Stribiżew
    Nov 22 '18 at 14:26













  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!

    – bukowski
    Nov 22 '18 at 14:51











  • Posted with explanation.

    – Wiktor Stribiżew
    Nov 22 '18 at 14:55














2












2








2








I have a file with contents like so:



## this must go ##
## also
this
must go
##
hello world
##and this one
too##


I want to remove all between ##, including multi lines, so I am left just with hello world



This removes only part that is on one line:



sed -i.bak 's/##.*##//g' myfile


How to remove multi line stuff too?



P.S Im on MAC










share|improve this question
















I have a file with contents like so:



## this must go ##
## also
this
must go
##
hello world
##and this one
too##


I want to remove all between ##, including multi lines, so I am left just with hello world



This removes only part that is on one line:



sed -i.bak 's/##.*##//g' myfile


How to remove multi line stuff too?



P.S Im on MAC







regex perl awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:47









RavinderSingh13

28.9k41638




28.9k41638










asked Nov 22 '18 at 14:16









bukowskibukowski

91862146




91862146













  • Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).

    – Wiktor Stribiżew
    Nov 22 '18 at 14:26













  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!

    – bukowski
    Nov 22 '18 at 14:51











  • Posted with explanation.

    – Wiktor Stribiżew
    Nov 22 '18 at 14:55



















  • Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).

    – Wiktor Stribiżew
    Nov 22 '18 at 14:26













  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!

    – bukowski
    Nov 22 '18 at 14:51











  • Posted with explanation.

    – Wiktor Stribiżew
    Nov 22 '18 at 14:55

















Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).

– Wiktor Stribiżew
Nov 22 '18 at 14:26







Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).

– Wiktor Stribiżew
Nov 22 '18 at 14:26















@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!

– bukowski
Nov 22 '18 at 14:51





@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!

– bukowski
Nov 22 '18 at 14:51













Posted with explanation.

– Wiktor Stribiżew
Nov 22 '18 at 14:55





Posted with explanation.

– Wiktor Stribiżew
Nov 22 '18 at 14:55












4 Answers
4






active

oldest

votes


















2














You may use perl to achieve what you want:



perl -0pe 's/##.*?##R*//gs' file > newfile


See the online demo



The 0 argument makes it possible to find matches across lines.



The pattern matches





  • ## - two # symbols


  • .*? - any 0+ chars (even line break chars due to the s modifier) as few as possible


  • ## - two # symbols


  • R* - any 0+ line break sequences.






share|improve this answer































    1














    It should be very easy task for awk(in case you are ok with it). Could you please try following, will add explanation shortly too.



    awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file


    Adding a non-one liner form of solution too now.



    awk '
    /^##.*##$/{
    next
    }
    /^##$/{
    flag=""
    next
    }
    /^##/ && !/##$/{
    flag=1
    }
    flag{
    next
    }
    1
    ' Input_file





    share|improve this answer































      1














      Give a try to this:



      sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile


      Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett



      The test:



      sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile

      hello world





      share|improve this answer

































        0














        This might work for you (GNU sed):



        sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file


        The -z option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ## followed by zero or more non-#'s. The second part matches a zero or more group of characters consisting of a single # followed by a non-# followed by zero or more non-#'s. The third part matches ## and a possible newline. This regexp removes such matches globally throughout the file.



        This can by shortened slightly by using the -r option to sweeten the final offering to:



        sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file


        If the version of sed does not offer either options, then another solution is:



        sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file


        It should by noted, that in the example above, all ##'s either start or end at the beginning or end of a line and so the solution below might also fit the bill:



        sed 's/^##/,/##$/d' file 





        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432910%2fremove-everything-between-specified-characters-including-multi-lines%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You may use perl to achieve what you want:



          perl -0pe 's/##.*?##R*//gs' file > newfile


          See the online demo



          The 0 argument makes it possible to find matches across lines.



          The pattern matches





          • ## - two # symbols


          • .*? - any 0+ chars (even line break chars due to the s modifier) as few as possible


          • ## - two # symbols


          • R* - any 0+ line break sequences.






          share|improve this answer




























            2














            You may use perl to achieve what you want:



            perl -0pe 's/##.*?##R*//gs' file > newfile


            See the online demo



            The 0 argument makes it possible to find matches across lines.



            The pattern matches





            • ## - two # symbols


            • .*? - any 0+ chars (even line break chars due to the s modifier) as few as possible


            • ## - two # symbols


            • R* - any 0+ line break sequences.






            share|improve this answer


























              2












              2








              2







              You may use perl to achieve what you want:



              perl -0pe 's/##.*?##R*//gs' file > newfile


              See the online demo



              The 0 argument makes it possible to find matches across lines.



              The pattern matches





              • ## - two # symbols


              • .*? - any 0+ chars (even line break chars due to the s modifier) as few as possible


              • ## - two # symbols


              • R* - any 0+ line break sequences.






              share|improve this answer













              You may use perl to achieve what you want:



              perl -0pe 's/##.*?##R*//gs' file > newfile


              See the online demo



              The 0 argument makes it possible to find matches across lines.



              The pattern matches





              • ## - two # symbols


              • .*? - any 0+ chars (even line break chars due to the s modifier) as few as possible


              • ## - two # symbols


              • R* - any 0+ line break sequences.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 14:53









              Wiktor StribiżewWiktor Stribiżew

              320k16140222




              320k16140222

























                  1














                  It should be very easy task for awk(in case you are ok with it). Could you please try following, will add explanation shortly too.



                  awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file


                  Adding a non-one liner form of solution too now.



                  awk '
                  /^##.*##$/{
                  next
                  }
                  /^##$/{
                  flag=""
                  next
                  }
                  /^##/ && !/##$/{
                  flag=1
                  }
                  flag{
                  next
                  }
                  1
                  ' Input_file





                  share|improve this answer




























                    1














                    It should be very easy task for awk(in case you are ok with it). Could you please try following, will add explanation shortly too.



                    awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file


                    Adding a non-one liner form of solution too now.



                    awk '
                    /^##.*##$/{
                    next
                    }
                    /^##$/{
                    flag=""
                    next
                    }
                    /^##/ && !/##$/{
                    flag=1
                    }
                    flag{
                    next
                    }
                    1
                    ' Input_file





                    share|improve this answer


























                      1












                      1








                      1







                      It should be very easy task for awk(in case you are ok with it). Could you please try following, will add explanation shortly too.



                      awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file


                      Adding a non-one liner form of solution too now.



                      awk '
                      /^##.*##$/{
                      next
                      }
                      /^##$/{
                      flag=""
                      next
                      }
                      /^##/ && !/##$/{
                      flag=1
                      }
                      flag{
                      next
                      }
                      1
                      ' Input_file





                      share|improve this answer













                      It should be very easy task for awk(in case you are ok with it). Could you please try following, will add explanation shortly too.



                      awk '/^##.*##$/{next} /^##$/{flag="";next} /^##/ && !/##$/{flag=1} flag{next} 1' Input_file


                      Adding a non-one liner form of solution too now.



                      awk '
                      /^##.*##$/{
                      next
                      }
                      /^##$/{
                      flag=""
                      next
                      }
                      /^##/ && !/##$/{
                      flag=1
                      }
                      flag{
                      next
                      }
                      1
                      ' Input_file






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 22 '18 at 14:27









                      RavinderSingh13RavinderSingh13

                      28.9k41638




                      28.9k41638























                          1














                          Give a try to this:



                          sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile


                          Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett



                          The test:



                          sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile

                          hello world





                          share|improve this answer






























                            1














                            Give a try to this:



                            sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile


                            Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett



                            The test:



                            sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile

                            hello world





                            share|improve this answer




























                              1












                              1








                              1







                              Give a try to this:



                              sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile


                              Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett



                              The test:



                              sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile

                              hello world





                              share|improve this answer















                              Give a try to this:



                              sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile


                              Wise men read this excellent tutorial: Sed - An Introduction and Tutorial by Bruce Barnett



                              The test:



                              sed -n '/^##/ { :1 ; /##$/ { d } ; n ; b 1 } ; p' myfile

                              hello world






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 22 '18 at 16:26

























                              answered Nov 22 '18 at 14:31









                              Jay jargotJay jargot

                              1,9491510




                              1,9491510























                                  0














                                  This might work for you (GNU sed):



                                  sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file


                                  The -z option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ## followed by zero or more non-#'s. The second part matches a zero or more group of characters consisting of a single # followed by a non-# followed by zero or more non-#'s. The third part matches ## and a possible newline. This regexp removes such matches globally throughout the file.



                                  This can by shortened slightly by using the -r option to sweeten the final offering to:



                                  sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file


                                  If the version of sed does not offer either options, then another solution is:



                                  sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file


                                  It should by noted, that in the example above, all ##'s either start or end at the beginning or end of a line and so the solution below might also fit the bill:



                                  sed 's/^##/,/##$/d' file 





                                  share|improve this answer






























                                    0














                                    This might work for you (GNU sed):



                                    sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file


                                    The -z option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ## followed by zero or more non-#'s. The second part matches a zero or more group of characters consisting of a single # followed by a non-# followed by zero or more non-#'s. The third part matches ## and a possible newline. This regexp removes such matches globally throughout the file.



                                    This can by shortened slightly by using the -r option to sweeten the final offering to:



                                    sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file


                                    If the version of sed does not offer either options, then another solution is:



                                    sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file


                                    It should by noted, that in the example above, all ##'s either start or end at the beginning or end of a line and so the solution below might also fit the bill:



                                    sed 's/^##/,/##$/d' file 





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      This might work for you (GNU sed):



                                      sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file


                                      The -z option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ## followed by zero or more non-#'s. The second part matches a zero or more group of characters consisting of a single # followed by a non-# followed by zero or more non-#'s. The third part matches ## and a possible newline. This regexp removes such matches globally throughout the file.



                                      This can by shortened slightly by using the -r option to sweeten the final offering to:



                                      sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file


                                      If the version of sed does not offer either options, then another solution is:



                                      sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file


                                      It should by noted, that in the example above, all ##'s either start or end at the beginning or end of a line and so the solution below might also fit the bill:



                                      sed 's/^##/,/##$/d' file 





                                      share|improve this answer















                                      This might work for you (GNU sed):



                                      sed -z 's/##[^#]*(#[^#][^#]*)*##n?//g' file


                                      The -z option allows the whole of the file to be slurped into sed's pattern space. The regexp match is in three parts. The first part matches ## followed by zero or more non-#'s. The second part matches a zero or more group of characters consisting of a single # followed by a non-# followed by zero or more non-#'s. The third part matches ## and a possible newline. This regexp removes such matches globally throughout the file.



                                      This can by shortened slightly by using the -r option to sweeten the final offering to:



                                      sed -rz 's/##[^#]*(#[^#]+)*##n?//g' file


                                      If the version of sed does not offer either options, then another solution is:



                                      sed 'H;$!d;x;s/.//;s/##[^#]*(#[^#][^#]*)*##n?//g' file


                                      It should by noted, that in the example above, all ##'s either start or end at the beginning or end of a line and so the solution below might also fit the bill:



                                      sed 's/^##/,/##$/d' file 






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 23 '18 at 8:52

























                                      answered Nov 23 '18 at 8:42









                                      potongpotong

                                      35.9k43061




                                      35.9k43061






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432910%2fremove-everything-between-specified-characters-including-multi-lines%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown