Grep for multiple strings, show number of lines after one of the strings (but not the other)












2















I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.



cat file | grep -e 'string1' -e 'string2'


works of course, but I would like to get something like



cat file | grep -e 'string1' -e -A 5 'string2'


How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?



EDIT: just got it working with awk:



cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'









share|improve this question















migrated from serverfault.com Sep 17 '15 at 9:52


This question came from our site for system and network administrators.























    2















    I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.



    cat file | grep -e 'string1' -e 'string2'


    works of course, but I would like to get something like



    cat file | grep -e 'string1' -e -A 5 'string2'


    How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?



    EDIT: just got it working with awk:



    cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'









    share|improve this question















    migrated from serverfault.com Sep 17 '15 at 9:52


    This question came from our site for system and network administrators.





















      2












      2








      2








      I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.



      cat file | grep -e 'string1' -e 'string2'


      works of course, but I would like to get something like



      cat file | grep -e 'string1' -e -A 5 'string2'


      How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?



      EDIT: just got it working with awk:



      cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'









      share|improve this question
















      I would like to search a file for multiple strings and for one particular string, show the 5 lines following it as well.



      cat file | grep -e 'string1' -e 'string2'


      works of course, but I would like to get something like



      cat file | grep -e 'string1' -e -A 5 'string2'


      How can I make it so that (I'm assuming using the -A 5 option) it shows only the lines containing string1 and only the lines containing string2 and the 5 lines after string2, but not the 5 lines after string1?



      EDIT: just got it working with awk:



      cat file | awk '/string1/{c=1}/string2/{c=5}{while(c-->0){print;getline}}'






      shell-script text-processing grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 15 at 5:15









      Rui F Ribeiro

      40.7k1479137




      40.7k1479137










      asked Sep 17 '15 at 9:47







      wtptrs











      migrated from serverfault.com Sep 17 '15 at 9:52


      This question came from our site for system and network administrators.









      migrated from serverfault.com Sep 17 '15 at 9:52


      This question came from our site for system and network administrators.
























          3 Answers
          3






          active

          oldest

          votes


















          0














          You can't do that using one go of grep. You need to use two grep instances :



          grep 'string1' file; grep -A 5 'string2' file


          If you want to run the second upon success of the first one :



          grep 'string1' file && grep -A 5 'string2' file





          share|improve this answer































            0














            about awk code



            awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file



            • do not cat ... | awk ,awk can read file.

            • I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)






            share|improve this answer
























            • To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

              – Costas
              Sep 17 '15 at 11:26











            • Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

              – Costas
              Sep 17 '15 at 11:32





















            0














            Sed solution:



            sed -n '/string2/,+4{p;b;};/string1/p' file


            my sed also accept ~ instead of +






            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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2funix.stackexchange.com%2fquestions%2f230241%2fgrep-for-multiple-strings-show-number-of-lines-after-one-of-the-strings-but-no%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown
























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can't do that using one go of grep. You need to use two grep instances :



              grep 'string1' file; grep -A 5 'string2' file


              If you want to run the second upon success of the first one :



              grep 'string1' file && grep -A 5 'string2' file





              share|improve this answer




























                0














                You can't do that using one go of grep. You need to use two grep instances :



                grep 'string1' file; grep -A 5 'string2' file


                If you want to run the second upon success of the first one :



                grep 'string1' file && grep -A 5 'string2' file





                share|improve this answer


























                  0












                  0








                  0







                  You can't do that using one go of grep. You need to use two grep instances :



                  grep 'string1' file; grep -A 5 'string2' file


                  If you want to run the second upon success of the first one :



                  grep 'string1' file && grep -A 5 'string2' file





                  share|improve this answer













                  You can't do that using one go of grep. You need to use two grep instances :



                  grep 'string1' file; grep -A 5 'string2' file


                  If you want to run the second upon success of the first one :



                  grep 'string1' file && grep -A 5 'string2' file






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 17 '15 at 10:24









                  heemaylheemayl

                  35.7k376105




                  35.7k376105

























                      0














                      about awk code



                      awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file



                      • do not cat ... | awk ,awk can read file.

                      • I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)






                      share|improve this answer
























                      • To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

                        – Costas
                        Sep 17 '15 at 11:26











                      • Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

                        – Costas
                        Sep 17 '15 at 11:32


















                      0














                      about awk code



                      awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file



                      • do not cat ... | awk ,awk can read file.

                      • I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)






                      share|improve this answer
























                      • To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

                        – Costas
                        Sep 17 '15 at 11:26











                      • Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

                        – Costas
                        Sep 17 '15 at 11:32
















                      0












                      0








                      0







                      about awk code



                      awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file



                      • do not cat ... | awk ,awk can read file.

                      • I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)






                      share|improve this answer













                      about awk code



                      awk '/string1/{if ( c<=0 ) c=1 ;}/string2/{c=5}{ if (c-->0) print;}' file



                      • do not cat ... | awk ,awk can read file.

                      • I rearrange test (if string1 is one line after string2, it interrupt printing in OP's solution)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 17 '15 at 10:46









                      ArchemarArchemar

                      20.2k93772




                      20.2k93772













                      • To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

                        – Costas
                        Sep 17 '15 at 11:26











                      • Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

                        – Costas
                        Sep 17 '15 at 11:32





















                      • To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

                        – Costas
                        Sep 17 '15 at 11:26











                      • Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

                        – Costas
                        Sep 17 '15 at 11:32



















                      To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

                      – Costas
                      Sep 17 '15 at 11:26





                      To much ifs… awk '/string2/{c=NR+5}/string2/,NR==c{print;next}/string1/'

                      – Costas
                      Sep 17 '15 at 11:26













                      Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

                      – Costas
                      Sep 17 '15 at 11:32







                      Your shorted variant is awk '/string2/{c=4;do{print;getline}while(c--)}/string1/'

                      – Costas
                      Sep 17 '15 at 11:32













                      0














                      Sed solution:



                      sed -n '/string2/,+4{p;b;};/string1/p' file


                      my sed also accept ~ instead of +






                      share|improve this answer




























                        0














                        Sed solution:



                        sed -n '/string2/,+4{p;b;};/string1/p' file


                        my sed also accept ~ instead of +






                        share|improve this answer


























                          0












                          0








                          0







                          Sed solution:



                          sed -n '/string2/,+4{p;b;};/string1/p' file


                          my sed also accept ~ instead of +






                          share|improve this answer













                          Sed solution:



                          sed -n '/string2/,+4{p;b;};/string1/p' file


                          my sed also accept ~ instead of +







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 17 '15 at 11:44









                          CostasCostas

                          12.7k1129




                          12.7k1129






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f230241%2fgrep-for-multiple-strings-show-number-of-lines-after-one-of-the-strings-but-no%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

                              How to make a Squid Proxy server?

                              Is this a new Fibonacci Identity?

                              19世紀