BSD sed vs GNU - is it capable of nested matches?

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











up vote
4
down vote

favorite












This works perfectly well on any Linux :



$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar


But fails on OSXs ancient BSD variant :



❯ echo foo bar | sed -n '/foo//bar/;p;' 
sed: 1: "/foo//bar/;p;": extra characters at the end of } command


Am I missing some magical incantation?
Is there a way to write this in a portable manner ?



I'd hate to have to revert to a pipeline of grep | grep | grep commands.



Update : low rep here so can't upvote but thanks all repliers for your well considered advice.







share|improve this question

















  • 1




    If you want the lines where all the keywords appear, you could also invert the condition to make it more linear: sed '/foo/!d; /bar/!d'
    – ilkkachu
    May 16 at 16:16










  • Great pointer @ilkkachu - thanks
    – Bryan Hunt
    May 17 at 10:24














up vote
4
down vote

favorite












This works perfectly well on any Linux :



$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar


But fails on OSXs ancient BSD variant :



❯ echo foo bar | sed -n '/foo//bar/;p;' 
sed: 1: "/foo//bar/;p;": extra characters at the end of } command


Am I missing some magical incantation?
Is there a way to write this in a portable manner ?



I'd hate to have to revert to a pipeline of grep | grep | grep commands.



Update : low rep here so can't upvote but thanks all repliers for your well considered advice.







share|improve this question

















  • 1




    If you want the lines where all the keywords appear, you could also invert the condition to make it more linear: sed '/foo/!d; /bar/!d'
    – ilkkachu
    May 16 at 16:16










  • Great pointer @ilkkachu - thanks
    – Bryan Hunt
    May 17 at 10:24












up vote
4
down vote

favorite









up vote
4
down vote

favorite











This works perfectly well on any Linux :



$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar


But fails on OSXs ancient BSD variant :



❯ echo foo bar | sed -n '/foo//bar/;p;' 
sed: 1: "/foo//bar/;p;": extra characters at the end of } command


Am I missing some magical incantation?
Is there a way to write this in a portable manner ?



I'd hate to have to revert to a pipeline of grep | grep | grep commands.



Update : low rep here so can't upvote but thanks all repliers for your well considered advice.







share|improve this question













This works perfectly well on any Linux :



$ echo foo bar | sed -n '/foo//bar/;p;'
foo bar


But fails on OSXs ancient BSD variant :



❯ echo foo bar | sed -n '/foo//bar/;p;' 
sed: 1: "/foo//bar/;p;": extra characters at the end of } command


Am I missing some magical incantation?
Is there a way to write this in a portable manner ?



I'd hate to have to revert to a pipeline of grep | grep | grep commands.



Update : low rep here so can't upvote but thanks all repliers for your well considered advice.









share|improve this question












share|improve this question




share|improve this question








edited May 17 at 8:15
























asked May 16 at 15:56









Bryan Hunt

234




234







  • 1




    If you want the lines where all the keywords appear, you could also invert the condition to make it more linear: sed '/foo/!d; /bar/!d'
    – ilkkachu
    May 16 at 16:16










  • Great pointer @ilkkachu - thanks
    – Bryan Hunt
    May 17 at 10:24












  • 1




    If you want the lines where all the keywords appear, you could also invert the condition to make it more linear: sed '/foo/!d; /bar/!d'
    – ilkkachu
    May 16 at 16:16










  • Great pointer @ilkkachu - thanks
    – Bryan Hunt
    May 17 at 10:24







1




1




If you want the lines where all the keywords appear, you could also invert the condition to make it more linear: sed '/foo/!d; /bar/!d'
– ilkkachu
May 16 at 16:16




If you want the lines where all the keywords appear, you could also invert the condition to make it more linear: sed '/foo/!d; /bar/!d'
– ilkkachu
May 16 at 16:16












Great pointer @ilkkachu - thanks
– Bryan Hunt
May 17 at 10:24




Great pointer @ilkkachu - thanks
– Bryan Hunt
May 17 at 10:24










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










A sed editing command should be terminated by ; or a literal newline. GNU sed is very forgiving about this.



Your script:



/foo//bar/;p;


Expanded:



/foo/
/bar/
p




This would work as a sed script fed to sed through -f.



If we make sure to replace newlines with ; (only needed at the end of commands and ... groups of commands) so that we can use it on the command line, we get



/foo//bar/p;;


This works with OpenBSD sed (the original did not, due to that second ; missing).



In this particular case, this may be further simplified to



/foo//bar/p;





share|improve this answer






























    up vote
    2
    down vote













    This appears to work on BSD sed:



    $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
    foo bar


    As does two layers of nesting:



    $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
    foo bar





    share|improve this answer





















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      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: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f444189%2fbsd-sed-vs-gnu-is-it-capable-of-nested-matches%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
      5
      down vote



      accepted










      A sed editing command should be terminated by ; or a literal newline. GNU sed is very forgiving about this.



      Your script:



      /foo//bar/;p;


      Expanded:



      /foo/
      /bar/
      p




      This would work as a sed script fed to sed through -f.



      If we make sure to replace newlines with ; (only needed at the end of commands and ... groups of commands) so that we can use it on the command line, we get



      /foo//bar/p;;


      This works with OpenBSD sed (the original did not, due to that second ; missing).



      In this particular case, this may be further simplified to



      /foo//bar/p;





      share|improve this answer



























        up vote
        5
        down vote



        accepted










        A sed editing command should be terminated by ; or a literal newline. GNU sed is very forgiving about this.



        Your script:



        /foo//bar/;p;


        Expanded:



        /foo/
        /bar/
        p




        This would work as a sed script fed to sed through -f.



        If we make sure to replace newlines with ; (only needed at the end of commands and ... groups of commands) so that we can use it on the command line, we get



        /foo//bar/p;;


        This works with OpenBSD sed (the original did not, due to that second ; missing).



        In this particular case, this may be further simplified to



        /foo//bar/p;





        share|improve this answer

























          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          A sed editing command should be terminated by ; or a literal newline. GNU sed is very forgiving about this.



          Your script:



          /foo//bar/;p;


          Expanded:



          /foo/
          /bar/
          p




          This would work as a sed script fed to sed through -f.



          If we make sure to replace newlines with ; (only needed at the end of commands and ... groups of commands) so that we can use it on the command line, we get



          /foo//bar/p;;


          This works with OpenBSD sed (the original did not, due to that second ; missing).



          In this particular case, this may be further simplified to



          /foo//bar/p;





          share|improve this answer















          A sed editing command should be terminated by ; or a literal newline. GNU sed is very forgiving about this.



          Your script:



          /foo//bar/;p;


          Expanded:



          /foo/
          /bar/
          p




          This would work as a sed script fed to sed through -f.



          If we make sure to replace newlines with ; (only needed at the end of commands and ... groups of commands) so that we can use it on the command line, we get



          /foo//bar/p;;


          This works with OpenBSD sed (the original did not, due to that second ; missing).



          In this particular case, this may be further simplified to



          /foo//bar/p;






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 16 at 16:19


























          answered May 16 at 16:02









          Kusalananda

          102k13199315




          102k13199315






















              up vote
              2
              down vote













              This appears to work on BSD sed:



              $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
              foo bar


              As does two layers of nesting:



              $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
              foo bar





              share|improve this answer

























                up vote
                2
                down vote













                This appears to work on BSD sed:



                $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
                foo bar


                As does two layers of nesting:



                $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
                foo bar





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  This appears to work on BSD sed:



                  $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
                  foo bar


                  As does two layers of nesting:



                  $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
                  foo bar





                  share|improve this answer













                  This appears to work on BSD sed:



                  $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;'
                  foo bar


                  As does two layers of nesting:



                  $ echo -e "foo barnfoonbar" | sed -n '/foo//bar/p;;'
                  foo bar






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 16 at 16:02









                  DopeGhoti

                  40k54779




                  40k54779






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444189%2fbsd-sed-vs-gnu-is-it-capable-of-nested-matches%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay