grep: detect multi-line pattern with double capture

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











up vote
1
down vote

favorite












I have file, say test.s that contains several trivial infinite loops:



.LBB7_7:
branch .LBB7_7


Labels may be totally different, but all are like .LBBd_d+



I want some neat way to process such things with grep or sed one-liner.



Now I am doing this way. First I calculate all labels:



grep -oP 'branch .KLBBd_d+' minimize.s


And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:" and checking its output for branch $i



Can I do better (without explicit bash processing with grep only)?







share|improve this question
























    up vote
    1
    down vote

    favorite












    I have file, say test.s that contains several trivial infinite loops:



    .LBB7_7:
    branch .LBB7_7


    Labels may be totally different, but all are like .LBBd_d+



    I want some neat way to process such things with grep or sed one-liner.



    Now I am doing this way. First I calculate all labels:



    grep -oP 'branch .KLBBd_d+' minimize.s


    And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:" and checking its output for branch $i



    Can I do better (without explicit bash processing with grep only)?







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have file, say test.s that contains several trivial infinite loops:



      .LBB7_7:
      branch .LBB7_7


      Labels may be totally different, but all are like .LBBd_d+



      I want some neat way to process such things with grep or sed one-liner.



      Now I am doing this way. First I calculate all labels:



      grep -oP 'branch .KLBBd_d+' minimize.s


      And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:" and checking its output for branch $i



      Can I do better (without explicit bash processing with grep only)?







      share|improve this question












      I have file, say test.s that contains several trivial infinite loops:



      .LBB7_7:
      branch .LBB7_7


      Labels may be totally different, but all are like .LBBd_d+



      I want some neat way to process such things with grep or sed one-liner.



      Now I am doing this way. First I calculate all labels:



      grep -oP 'branch .KLBBd_d+' minimize.s


      And then in bash for loop I looking up label with one line below with grep -A1 "^.$i:" and checking its output for branch $i



      Can I do better (without explicit bash processing with grep only)?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 14 '17 at 9:11









      Konstantin Vladimirov

      1084




      1084




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          try this:



          $ cat ip.txt
          foo
          baz
          .LBB7_7:
          branch .LBB7_7
          xyzyadsf
          .LBB8_3:
          mov a, b
          branch .LBB8_3
          nop

          $ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
          .LBB7_7:
          branch .LBB7_7



          • -z will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped


          • (.LBBd_d+) capture label, but can't specify to match at start of line


          • :s*branchh+1n condition to check for infinite loop





          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%2f410824%2fgrep-detect-multi-line-pattern-with-double-capture%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            try this:



            $ cat ip.txt
            foo
            baz
            .LBB7_7:
            branch .LBB7_7
            xyzyadsf
            .LBB8_3:
            mov a, b
            branch .LBB8_3
            nop

            $ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
            .LBB7_7:
            branch .LBB7_7



            • -z will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped


            • (.LBBd_d+) capture label, but can't specify to match at start of line


            • :s*branchh+1n condition to check for infinite loop





            share|improve this answer
























              up vote
              3
              down vote



              accepted










              try this:



              $ cat ip.txt
              foo
              baz
              .LBB7_7:
              branch .LBB7_7
              xyzyadsf
              .LBB8_3:
              mov a, b
              branch .LBB8_3
              nop

              $ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
              .LBB7_7:
              branch .LBB7_7



              • -z will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped


              • (.LBBd_d+) capture label, but can't specify to match at start of line


              • :s*branchh+1n condition to check for infinite loop





              share|improve this answer






















                up vote
                3
                down vote



                accepted







                up vote
                3
                down vote



                accepted






                try this:



                $ cat ip.txt
                foo
                baz
                .LBB7_7:
                branch .LBB7_7
                xyzyadsf
                .LBB8_3:
                mov a, b
                branch .LBB8_3
                nop

                $ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
                .LBB7_7:
                branch .LBB7_7



                • -z will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped


                • (.LBBd_d+) capture label, but can't specify to match at start of line


                • :s*branchh+1n condition to check for infinite loop





                share|improve this answer












                try this:



                $ cat ip.txt
                foo
                baz
                .LBB7_7:
                branch .LBB7_7
                xyzyadsf
                .LBB8_3:
                mov a, b
                branch .LBB8_3
                nop

                $ grep -zoP '(.LBBd_d+):s*branchh+1n' ip.txt
                .LBB7_7:
                branch .LBB7_7



                • -z will use ASCII NUL as record separator instead of default newline character. Assuming your input doesn't have NUL characters, this will cause whole file to be slurped


                • (.LBBd_d+) capture label, but can't specify to match at start of line


                • :s*branchh+1n condition to check for infinite loop






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 14 '17 at 9:24









                Sundeep

                6,9511826




                6,9511826






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410824%2fgrep-detect-multi-line-pattern-with-double-capture%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