grep pattern before another pattern and print it all

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











up vote
4
down vote

favorite












Given intput:



Via: 1.1.1.1 
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 2.2.2.2
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
Via: 3.3.3.3
not relevant lines
Via: 4.4.4.4
not relevant
Via: 5.5.5.5
not relevant line
keyword + some text
...
not relevant line N
keyword + some text
...
not relevant line N
not relevant line N
...


Required output:



Via: 1.1.1.1 
keyword + some text A
keyword + some text A
Via: 2.2.2.2
keyword + some text B
keyword + some text C
Via: 5.5.5.5
keyword + some text D
keyword + some text E


keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.










share|improve this question



























    up vote
    4
    down vote

    favorite












    Given intput:



    Via: 1.1.1.1 
    not relevant line
    keyword + some text
    ...
    not relevant line N
    keyword + some text
    ...
    not relevant line N
    Via: 2.2.2.2
    not relevant line
    keyword + some text
    ...
    not relevant line N
    keyword + some text
    ...
    not relevant line N
    Via: 3.3.3.3
    not relevant lines
    Via: 4.4.4.4
    not relevant
    Via: 5.5.5.5
    not relevant line
    keyword + some text
    ...
    not relevant line N
    keyword + some text
    ...
    not relevant line N
    not relevant line N
    ...


    Required output:



    Via: 1.1.1.1 
    keyword + some text A
    keyword + some text A
    Via: 2.2.2.2
    keyword + some text B
    keyword + some text C
    Via: 5.5.5.5
    keyword + some text D
    keyword + some text E


    keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.










    share|improve this question

























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Given intput:



      Via: 1.1.1.1 
      not relevant line
      keyword + some text
      ...
      not relevant line N
      keyword + some text
      ...
      not relevant line N
      Via: 2.2.2.2
      not relevant line
      keyword + some text
      ...
      not relevant line N
      keyword + some text
      ...
      not relevant line N
      Via: 3.3.3.3
      not relevant lines
      Via: 4.4.4.4
      not relevant
      Via: 5.5.5.5
      not relevant line
      keyword + some text
      ...
      not relevant line N
      keyword + some text
      ...
      not relevant line N
      not relevant line N
      ...


      Required output:



      Via: 1.1.1.1 
      keyword + some text A
      keyword + some text A
      Via: 2.2.2.2
      keyword + some text B
      keyword + some text C
      Via: 5.5.5.5
      keyword + some text D
      keyword + some text E


      keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.










      share|improve this question















      Given intput:



      Via: 1.1.1.1 
      not relevant line
      keyword + some text
      ...
      not relevant line N
      keyword + some text
      ...
      not relevant line N
      Via: 2.2.2.2
      not relevant line
      keyword + some text
      ...
      not relevant line N
      keyword + some text
      ...
      not relevant line N
      Via: 3.3.3.3
      not relevant lines
      Via: 4.4.4.4
      not relevant
      Via: 5.5.5.5
      not relevant line
      keyword + some text
      ...
      not relevant line N
      keyword + some text
      ...
      not relevant line N
      not relevant line N
      ...


      Required output:



      Via: 1.1.1.1 
      keyword + some text A
      keyword + some text A
      Via: 2.2.2.2
      keyword + some text B
      keyword + some text C
      Via: 5.5.5.5
      keyword + some text D
      keyword + some text E


      keyword string can occur N times in any Via block, or may not occur at all. In the output I need only those Via blocks where keyword occurs together with keyword strings belonging to them. The closest answer I found is here, but I can't make it into what I need.







      text-processing awk sed grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 1 '17 at 19:34









      don_crissti

      47k15124154




      47k15124154










      asked Oct 1 '17 at 19:09









      Olga

      150114




      150114




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          With sed:



          sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt


          Or, if you want keyword anchored at the beginning of line:



          sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt





          share|improve this answer




















          • Many thanks @Sato Katsura
            – Olga
            Oct 1 '17 at 19:44

















          up vote
          4
          down vote













          Awk solution:



          awk '/^Via:/ f=1; r=$0; kw=0; next 
          f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file



          • /^Via:/ - capturing line starting with Via: into r variable. Set flag f=1 into "active" state indicating processing of a certain Via block


          • kw - flag denoting the number of "keyword" lines under each Via block


          • f && /keyword/ - while processing lines under Via block - consider only lines matching keyword pattern






          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%2f395509%2fgrep-pattern-before-another-pattern-and-print-it-all%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










            With sed:



            sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt


            Or, if you want keyword anchored at the beginning of line:



            sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt





            share|improve this answer




















            • Many thanks @Sato Katsura
              – Olga
              Oct 1 '17 at 19:44














            up vote
            5
            down vote



            accepted










            With sed:



            sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt


            Or, if you want keyword anchored at the beginning of line:



            sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt





            share|improve this answer




















            • Many thanks @Sato Katsura
              – Olga
              Oct 1 '17 at 19:44












            up vote
            5
            down vote



            accepted







            up vote
            5
            down vote



            accepted






            With sed:



            sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt


            Or, if you want keyword anchored at the beginning of line:



            sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt





            share|improve this answer












            With sed:



            sed -n '/^Via:/ x; /keyword/p; d; ; /keyword/H; $ x; /keyword/p; ' input.txt


            Or, if you want keyword anchored at the beginning of line:



            sed -n '/^Via:/ x; /nkeyword/p; d; ; /^keyword/H; $ x; /nkeyword/p; ' input.txt






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 1 '17 at 19:27









            Satō Katsura

            10.7k11533




            10.7k11533











            • Many thanks @Sato Katsura
              – Olga
              Oct 1 '17 at 19:44
















            • Many thanks @Sato Katsura
              – Olga
              Oct 1 '17 at 19:44















            Many thanks @Sato Katsura
            – Olga
            Oct 1 '17 at 19:44




            Many thanks @Sato Katsura
            – Olga
            Oct 1 '17 at 19:44












            up vote
            4
            down vote













            Awk solution:



            awk '/^Via:/ f=1; r=$0; kw=0; next 
            f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file



            • /^Via:/ - capturing line starting with Via: into r variable. Set flag f=1 into "active" state indicating processing of a certain Via block


            • kw - flag denoting the number of "keyword" lines under each Via block


            • f && /keyword/ - while processing lines under Via block - consider only lines matching keyword pattern






            share|improve this answer


























              up vote
              4
              down vote













              Awk solution:



              awk '/^Via:/ f=1; r=$0; kw=0; next 
              f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file



              • /^Via:/ - capturing line starting with Via: into r variable. Set flag f=1 into "active" state indicating processing of a certain Via block


              • kw - flag denoting the number of "keyword" lines under each Via block


              • f && /keyword/ - while processing lines under Via block - consider only lines matching keyword pattern






              share|improve this answer
























                up vote
                4
                down vote










                up vote
                4
                down vote









                Awk solution:



                awk '/^Via:/ f=1; r=$0; kw=0; next 
                f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file



                • /^Via:/ - capturing line starting with Via: into r variable. Set flag f=1 into "active" state indicating processing of a certain Via block


                • kw - flag denoting the number of "keyword" lines under each Via block


                • f && /keyword/ - while processing lines under Via block - consider only lines matching keyword pattern






                share|improve this answer














                Awk solution:



                awk '/^Via:/ f=1; r=$0; kw=0; next 
                f && /keyword/ printf "%s%sn",(!kw)? r ORS:"",$0; kw++ ' file



                • /^Via:/ - capturing line starting with Via: into r variable. Set flag f=1 into "active" state indicating processing of a certain Via block


                • kw - flag denoting the number of "keyword" lines under each Via block


                • f && /keyword/ - while processing lines under Via block - consider only lines matching keyword pattern







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 1 '17 at 20:01

























                answered Oct 1 '17 at 19:42









                RomanPerekhrest

                22.5k12145




                22.5k12145



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395509%2fgrep-pattern-before-another-pattern-and-print-it-all%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?