Remove everything between specified characters including multi lines











up vote
2
down vote

favorite












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 at 14:26












  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
    – bukowski
    Nov 22 at 14:51










  • Posted with explanation.
    – Wiktor Stribiżew
    Nov 22 at 14:55















up vote
2
down vote

favorite












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 at 14:26












  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
    – bukowski
    Nov 22 at 14:51










  • Posted with explanation.
    – Wiktor Stribiżew
    Nov 22 at 14:55













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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 at 15:47









RavinderSingh13

24.8k41437




24.8k41437










asked Nov 22 at 14:16









bukowski

87862144




87862144












  • Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).
    – Wiktor Stribiżew
    Nov 22 at 14:26












  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
    – bukowski
    Nov 22 at 14:51










  • Posted with explanation.
    – Wiktor Stribiżew
    Nov 22 at 14:55


















  • Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).
    – Wiktor Stribiżew
    Nov 22 at 14:26












  • @WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
    – bukowski
    Nov 22 at 14:51










  • Posted with explanation.
    – Wiktor Stribiżew
    Nov 22 at 14:55
















Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).
– Wiktor Stribiżew
Nov 22 at 14:26






Probably perl solution will be simpler: perl -0pe 's/##.*?##R*//gs' file > newfile (demo).
– Wiktor Stribiżew
Nov 22 at 14:26














@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51




@WiktorStribiżew hey your solution works the best - post your answer & I'll accept it. Thanks!
– bukowski
Nov 22 at 14:51












Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55




Posted with explanation.
– Wiktor Stribiżew
Nov 22 at 14:55












4 Answers
4






active

oldest

votes

















up vote
2
down vote



accepted










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




























    up vote
    1
    down vote













    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




























      up vote
      1
      down vote













      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






























        up vote
        0
        down vote













        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',
          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








          up vote
          2
          down vote



          accepted










          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

























            up vote
            2
            down vote



            accepted










            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























              up vote
              2
              down vote



              accepted







              up vote
              2
              down vote



              accepted






              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 at 14:53









              Wiktor Stribiżew

              304k16123200




              304k16123200
























                  up vote
                  1
                  down vote













                  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

























                    up vote
                    1
                    down vote













                    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























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      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 at 14:27









                      RavinderSingh13

                      24.8k41437




                      24.8k41437






















                          up vote
                          1
                          down vote













                          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



























                            up vote
                            1
                            down vote













                            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

























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              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 at 16:26

























                              answered Nov 22 at 14:31









                              Jay jargot

                              1,8771410




                              1,8771410






















                                  up vote
                                  0
                                  down vote













                                  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



























                                    up vote
                                    0
                                    down vote













                                    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

























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      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 at 8:52

























                                      answered Nov 23 at 8:42









                                      potong

                                      34.8k42960




                                      34.8k42960






























                                          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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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







                                          Popular posts from this blog

                                          What visual should I use to simply compare current year value vs last year in Power BI desktop

                                          Alexandru Averescu

                                          Trompette piccolo