How to get the n-th line after a grepped one?How can I set up an alias or shortcut to do the *nix find command?Awk responds differently based on how an empty argument is specifiedHow do different operators deal with a given stringConverting multiple files using programs that accepts stdin and output to stdoutBash script not waiting for wget to finish downloadSeeing actual commands typed in bash historyQueue a Bash script to run after currently job finishes?regex to match any combination of text of indeterminate lengthbash: how to print multiple arrays valuesSetting environment variables for Windows cmd temporarily, to run a program, in a single line?

The German vowel “a” changes to the English “i”

Is a party consisting of only a bard, a cleric, and a warlock functional long-term?

World War I as a war of liberals against authoritarians?

Why does a Star of David appear at a rally with Francisco Franco?

Book about superhumans hiding among normal humans

Meme-controlled people

Have the tides ever turned twice on any open problem?

Math equation in non italic font

What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?

A diagram about partial derivatives of f(x,y)

Bacteria contamination inside a thermos bottle

Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible

et qui - how do you really understand that kind of phraseology?

Why does overlay work only on the first tcolorbox?

Welcoming 2019 Pi day: How to draw the letter π?

Why do passenger jet manufacturers design their planes with stall prevention systems?

Bach's Toccata and Fugue in D minor breaks the "no parallel octaves" rule?

How do you talk to someone whose loved one is dying?

Can I use USB data pins as a power source?

"of which" is correct here?

Are all passive ability checks floors for active ability checks?

I am confused as to how the inverse of a certain function is found.

How to deal with taxi scam when on vacation?

Non-trivial topology where only open sets are closed



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


How can I set up an alias or shortcut to do the *nix find command?Awk responds differently based on how an empty argument is specifiedHow do different operators deal with a given stringConverting multiple files using programs that accepts stdin and output to stdoutBash script not waiting for wget to finish downloadSeeing actual commands typed in bash historyQueue a Bash script to run after currently job finishes?regex to match any combination of text of indeterminate lengthbash: how to print multiple arrays valuesSetting environment variables for Windows cmd temporarily, to run a program, in a single line?













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

















  • 1





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

    – Nasir Riley
    4 hours ago











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

    – MZB
    3 hours ago















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

















  • 1





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

    – Nasir Riley
    4 hours ago











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

    – MZB
    3 hours ago













1












1








1


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 4 hours ago









WoJWoJ

82232238




82232238







  • 1





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

    – Nasir Riley
    4 hours ago











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

    – MZB
    3 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
    4 hours ago











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

    – MZB
    3 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
4 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
4 hours ago













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

– MZB
3 hours ago





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

– MZB
3 hours ago










4 Answers
4






active

oldest

votes


















3














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
    37 mins ago


















1














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






























    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
      1 hour ago


















    0














    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






















      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









      3














      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
        37 mins ago















      3














      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
        37 mins ago













      3












      3








      3







      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 2 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
        37 mins ago

















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

        – Kamil Maciorowski
        37 mins ago
















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

      – Kamil Maciorowski
      37 mins ago





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

      – Kamil Maciorowski
      37 mins ago













      1














      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














        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












          1








          1







          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 4 hours ago









          zx485zx485

          1,0211913




          1,0211913





















              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
                1 hour 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
                1 hour 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 3 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
                1 hour ago

















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

                – phuclv
                1 hour ago
















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

              – phuclv
              1 hour ago





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

              – phuclv
              1 hour ago











              0














              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














                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












                  0








                  0







                  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 41 mins ago









                  Kamil MaciorowskiKamil Maciorowski

                  28.3k156186




                  28.3k156186



























                      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

                      Möglingen Índice Localización Historia Demografía Referencias Enlaces externos Menú de navegación48°53′18″N 9°07′45″E / 48.888333333333, 9.129166666666748°53′18″N 9°07′45″E / 48.888333333333, 9.1291666666667Sitio web oficial Mapa de Möglingen«Gemeinden in Deutschland nach Fläche, Bevölkerung und Postleitzahl am 30.09.2016»Möglingen

                      Virtualbox - Configuration error: Querying “UUID” failed (VERR_CFGM_VALUE_NOT_FOUND)“VERR_SUPLIB_WORLD_WRITABLE” error when trying to installing OS in virtualboxVirtual Box Kernel errorFailed to open a seesion for the virtual machineFailed to open a session for the virtual machineUbuntu 14.04 LTS Virtualbox errorcan't use VM VirtualBoxusing virtualboxI can't run Linux-64 Bit on VirtualBoxUnable to insert the virtual optical disk (VBoxguestaddition) in virtual machine for ubuntu server in win 10VirtuaBox in Ubuntu 18.04 Issues with Win10.ISO Installation

                      Torre de la Isleta Índice Véase también Referencias Bibliografía Enlaces externos Menú de navegación38°25′58″N 0°23′02″O / 38.43277778, -0.3838888938°25′58″N 0°23′02″O / 38.43277778, -0.38388889Torre de la Illeta de l’Horta o Torre Saleta. Base de datos de bienes inmuebles. Patrimonio Cultural. Secretaría de Estado de CulturaFicha BIC Torre de la Illeta de l’Horta. Dirección General de Patrimonio Cultural. Generalitat ValencianaLugares de interés. Ayuntamiento del CampelloTorre de la Isleta en CastillosNet.org