How to get the n-th line after a grepped one?












2















Consider the following text file



one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8


I would like to access to the second line after the one which matched four. This would be the line



six 6


The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2).



Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)










share|improve this question


















  • 1





    Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?

    – Nasir Riley
    7 hours ago











  • Use awk? But if you're more familiar with Python that would be quicker.

    – MZB
    6 hours ago
















2















Consider the following text file



one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8


I would like to access to the second line after the one which matched four. This would be the line



six 6


The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2).



Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)










share|improve this question


















  • 1





    Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?

    – Nasir Riley
    7 hours ago











  • Use awk? But if you're more familiar with Python that would be quicker.

    – MZB
    6 hours ago














2












2








2


1






Consider the following text file



one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8


I would like to access to the second line after the one which matched four. This would be the line



six 6


The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2).



Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)










share|improve this question














Consider the following text file



one 1
two 2
three 3
four 4
five 5
six 6
seven 7
eight 8


I would like to access to the second line after the one which matched four. This would be the line



six 6


The resulting line (so the one above) would then be piped for further processing (say, a | cut -d' ' -f2).



Is there a way to do this in bash and other typical utilities? (otherwise I will script it in Python)







linux command-line bash grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 7 hours ago









WoJWoJ

82732238




82732238








  • 1





    Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?

    – Nasir Riley
    7 hours ago











  • Use awk? But if you're more familiar with Python that would be quicker.

    – MZB
    6 hours ago














  • 1





    Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?

    – Nasir Riley
    7 hours ago











  • Use awk? But if you're more familiar with Python that would be quicker.

    – MZB
    6 hours ago








1




1





Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?

– Nasir Riley
7 hours ago





Will there always only be one occurrence of what you are trying to match? Will it always be two lines after?

– Nasir Riley
7 hours ago













Use awk? But if you're more familiar with Python that would be quicker.

– MZB
6 hours ago





Use awk? But if you're more familiar with Python that would be quicker.

– MZB
6 hours ago










4 Answers
4






active

oldest

votes


















5














There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed call:



sed -n "/four/ { n; n; p }" SourceData.txt


Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.



To match the first instance only:



sed -n "/four/ { n; n; p; q }" SourceData.txt


I add this solution because it is worth getting to know sed and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.






share|improve this answer
























  • "the other solutions don't handle this case in the expected manner either" – No longer true. :)

    – Kamil Maciorowski
    3 hours ago



















2














You can use this expression (input.txt):



grep "four" -A 2 input.txt | tail -n 1


Output is:



six 6


The grep option "-A 2" states that two lines after the matched line are outputted.

And the tail option "-n 1" states that only the last 1 lines of this result are returned.






share|improve this answer































    1














    This solution prints the current line iff there was a match two lines ago. It is slightly different from other answers (so far) because it won't miss another match even if it occurs soon after the previous match.



    awk -v delay=2 '{for (i=delay; i>=0; i--) t[i]=t[i-1]}; /four/ {t[0]="m"}; {if (t[delay]) print}'


    Whenever there's a match, the information is stored in t[0]. With each line the t array is shifted (including shifting t[-1] to t[0] to reset the value of t[0]). The line is printed iff the array indicates there was a match two lines ago.



    You can easily set a different delay (e.g. delay=7) or use another pattern (e.g. /sda[[:digit:]]/)






    share|improve this answer































      0














      For multiple occurences, and assumming that no lines start with --:



      ( grep -A 2  pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'


      In slo-mo:





      • ( grep -A 2 pattern data.txt; echo '--' ) prints the pattern and the next two lines, and inserts a -- line between the groups. echo '--' makes sure that the last group is also followed by --.


      • grep -E -B1 '^--' print the separators and the lines just before (whoch are the ones we are looking for)


      • grep -Ev '^--' drops the separators, leaving only the lines we are looking for.






      share|improve this answer
























      • if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

        – phuclv
        5 hours ago











      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "3"
      };
      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: 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%2fsuperuser.com%2fquestions%2f1414661%2fhow-to-get-the-n-th-line-after-a-grepped-one%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









      5














      There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed call:



      sed -n "/four/ { n; n; p }" SourceData.txt


      Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.



      To match the first instance only:



      sed -n "/four/ { n; n; p; q }" SourceData.txt


      I add this solution because it is worth getting to know sed and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.






      share|improve this answer
























      • "the other solutions don't handle this case in the expected manner either" – No longer true. :)

        – Kamil Maciorowski
        3 hours ago
















      5














      There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed call:



      sed -n "/four/ { n; n; p }" SourceData.txt


      Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.



      To match the first instance only:



      sed -n "/four/ { n; n; p; q }" SourceData.txt


      I add this solution because it is worth getting to know sed and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.






      share|improve this answer
























      • "the other solutions don't handle this case in the expected manner either" – No longer true. :)

        – Kamil Maciorowski
        3 hours ago














      5












      5








      5







      There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed call:



      sed -n "/four/ { n; n; p }" SourceData.txt


      Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.



      To match the first instance only:



      sed -n "/four/ { n; n; p; q }" SourceData.txt


      I add this solution because it is worth getting to know sed and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.






      share|improve this answer













      There is nothing wrong with the previous two answers, but I thought I would make you aware that finding the third line after a pattern can be done in a single sed call:



      sed -n "/four/ { n; n; p }" SourceData.txt


      Because a single program does the work, this is more efficient than running multiple filters. The above command outputs the third line after every instance of "four", except where this occurs again in one of the two lines following a match (the other solutions don't handle this case in the expected manner either); also, no output is generated if the pattern is in the last or second-last line of the file, which may or may not be what you want.



      To match the first instance only:



      sed -n "/four/ { n; n; p; q }" SourceData.txt


      I add this solution because it is worth getting to know sed and, despite its rather off-putting syntax (regular expressions are bad enough!), it can often be extremely useful. This tutorial is a good introduction.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 5 hours ago









      AFHAFH

      14.4k31938




      14.4k31938













      • "the other solutions don't handle this case in the expected manner either" – No longer true. :)

        – Kamil Maciorowski
        3 hours ago



















      • "the other solutions don't handle this case in the expected manner either" – No longer true. :)

        – Kamil Maciorowski
        3 hours ago

















      "the other solutions don't handle this case in the expected manner either" – No longer true. :)

      – Kamil Maciorowski
      3 hours ago





      "the other solutions don't handle this case in the expected manner either" – No longer true. :)

      – Kamil Maciorowski
      3 hours ago













      2














      You can use this expression (input.txt):



      grep "four" -A 2 input.txt | tail -n 1


      Output is:



      six 6


      The grep option "-A 2" states that two lines after the matched line are outputted.

      And the tail option "-n 1" states that only the last 1 lines of this result are returned.






      share|improve this answer




























        2














        You can use this expression (input.txt):



        grep "four" -A 2 input.txt | tail -n 1


        Output is:



        six 6


        The grep option "-A 2" states that two lines after the matched line are outputted.

        And the tail option "-n 1" states that only the last 1 lines of this result are returned.






        share|improve this answer


























          2












          2








          2







          You can use this expression (input.txt):



          grep "four" -A 2 input.txt | tail -n 1


          Output is:



          six 6


          The grep option "-A 2" states that two lines after the matched line are outputted.

          And the tail option "-n 1" states that only the last 1 lines of this result are returned.






          share|improve this answer













          You can use this expression (input.txt):



          grep "four" -A 2 input.txt | tail -n 1


          Output is:



          six 6


          The grep option "-A 2" states that two lines after the matched line are outputted.

          And the tail option "-n 1" states that only the last 1 lines of this result are returned.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 7 hours ago









          zx485zx485

          1,0411913




          1,0411913























              1














              This solution prints the current line iff there was a match two lines ago. It is slightly different from other answers (so far) because it won't miss another match even if it occurs soon after the previous match.



              awk -v delay=2 '{for (i=delay; i>=0; i--) t[i]=t[i-1]}; /four/ {t[0]="m"}; {if (t[delay]) print}'


              Whenever there's a match, the information is stored in t[0]. With each line the t array is shifted (including shifting t[-1] to t[0] to reset the value of t[0]). The line is printed iff the array indicates there was a match two lines ago.



              You can easily set a different delay (e.g. delay=7) or use another pattern (e.g. /sda[[:digit:]]/)






              share|improve this answer




























                1














                This solution prints the current line iff there was a match two lines ago. It is slightly different from other answers (so far) because it won't miss another match even if it occurs soon after the previous match.



                awk -v delay=2 '{for (i=delay; i>=0; i--) t[i]=t[i-1]}; /four/ {t[0]="m"}; {if (t[delay]) print}'


                Whenever there's a match, the information is stored in t[0]. With each line the t array is shifted (including shifting t[-1] to t[0] to reset the value of t[0]). The line is printed iff the array indicates there was a match two lines ago.



                You can easily set a different delay (e.g. delay=7) or use another pattern (e.g. /sda[[:digit:]]/)






                share|improve this answer


























                  1












                  1








                  1







                  This solution prints the current line iff there was a match two lines ago. It is slightly different from other answers (so far) because it won't miss another match even if it occurs soon after the previous match.



                  awk -v delay=2 '{for (i=delay; i>=0; i--) t[i]=t[i-1]}; /four/ {t[0]="m"}; {if (t[delay]) print}'


                  Whenever there's a match, the information is stored in t[0]. With each line the t array is shifted (including shifting t[-1] to t[0] to reset the value of t[0]). The line is printed iff the array indicates there was a match two lines ago.



                  You can easily set a different delay (e.g. delay=7) or use another pattern (e.g. /sda[[:digit:]]/)






                  share|improve this answer













                  This solution prints the current line iff there was a match two lines ago. It is slightly different from other answers (so far) because it won't miss another match even if it occurs soon after the previous match.



                  awk -v delay=2 '{for (i=delay; i>=0; i--) t[i]=t[i-1]}; /four/ {t[0]="m"}; {if (t[delay]) print}'


                  Whenever there's a match, the information is stored in t[0]. With each line the t array is shifted (including shifting t[-1] to t[0] to reset the value of t[0]). The line is printed iff the array indicates there was a match two lines ago.



                  You can easily set a different delay (e.g. delay=7) or use another pattern (e.g. /sda[[:digit:]]/)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 3 hours ago









                  Kamil MaciorowskiKamil Maciorowski

                  28.3k156186




                  28.3k156186























                      0














                      For multiple occurences, and assumming that no lines start with --:



                      ( grep -A 2  pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'


                      In slo-mo:





                      • ( grep -A 2 pattern data.txt; echo '--' ) prints the pattern and the next two lines, and inserts a -- line between the groups. echo '--' makes sure that the last group is also followed by --.


                      • grep -E -B1 '^--' print the separators and the lines just before (whoch are the ones we are looking for)


                      • grep -Ev '^--' drops the separators, leaving only the lines we are looking for.






                      share|improve this answer
























                      • if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

                        – phuclv
                        5 hours ago
















                      0














                      For multiple occurences, and assumming that no lines start with --:



                      ( grep -A 2  pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'


                      In slo-mo:





                      • ( grep -A 2 pattern data.txt; echo '--' ) prints the pattern and the next two lines, and inserts a -- line between the groups. echo '--' makes sure that the last group is also followed by --.


                      • grep -E -B1 '^--' print the separators and the lines just before (whoch are the ones we are looking for)


                      • grep -Ev '^--' drops the separators, leaving only the lines we are looking for.






                      share|improve this answer
























                      • if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

                        – phuclv
                        5 hours ago














                      0












                      0








                      0







                      For multiple occurences, and assumming that no lines start with --:



                      ( grep -A 2  pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'


                      In slo-mo:





                      • ( grep -A 2 pattern data.txt; echo '--' ) prints the pattern and the next two lines, and inserts a -- line between the groups. echo '--' makes sure that the last group is also followed by --.


                      • grep -E -B1 '^--' print the separators and the lines just before (whoch are the ones we are looking for)


                      • grep -Ev '^--' drops the separators, leaving only the lines we are looking for.






                      share|improve this answer













                      For multiple occurences, and assumming that no lines start with --:



                      ( grep -A 2  pattern data.txt; echo '--' ) | grep -E -B1 '^--' | grep -Ev '^--'


                      In slo-mo:





                      • ( grep -A 2 pattern data.txt; echo '--' ) prints the pattern and the next two lines, and inserts a -- line between the groups. echo '--' makes sure that the last group is also followed by --.


                      • grep -E -B1 '^--' print the separators and the lines just before (whoch are the ones we are looking for)


                      • grep -Ev '^--' drops the separators, leaving only the lines we are looking for.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 6 hours ago









                      xenoidxenoid

                      3,8193719




                      3,8193719













                      • if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

                        – phuclv
                        5 hours ago



















                      • if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

                        – phuclv
                        5 hours ago

















                      if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

                      – phuclv
                      5 hours ago





                      if you use the -A, -B or -C option then grep automatically use -- to separate matches, no need to print it yourself

                      – phuclv
                      5 hours ago


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Super User!


                      • 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%2fsuperuser.com%2fquestions%2f1414661%2fhow-to-get-the-n-th-line-after-a-grepped-one%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

                      Сан-Квентин

                      8-я гвардейская общевойсковая армия

                      Алькесар