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?

When to use a slotted vs. solid turner?

Why do tuner card drivers fail to build after kernel update to 4.4.0-143-generic?

How to plot polar formed complex numbers?

How to write cleanly even if my character uses expletive language?

Did Ender ever learn that he killed Stilson and/or Bonzo?

Why do newer 737s use two different styles of split winglets?

Happy pi day, everyone!

Simplify an interface for flexibly applying rules to periods of time

How to terminate ping <dest> &

Official degrees of earth’s rotation per day

Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?

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

How to make healing in an exploration game interesting

Employee lack of ownership

Instead of a Universal Basic Income program, why not implement a "Universal Basic Needs" program?

What options are left, if Britain cannot decide?

Aluminum electrolytic or ceramic capacitors for linear regulator input and output?

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

What's the meaning of a knight fighting a snail in medieval book illustrations?

Can I use USB data pins as a power source?

Professor being mistaken for a grad student

Do I need to be arrogant to get ahead?

What are substitutions for coconut in curry?

What is the purpose or proof behind chain rule?



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
    44 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
      2 hours 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
        44 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
        44 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
        44 mins ago

















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

        – Kamil Maciorowski
        44 mins ago
















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

      – Kamil Maciorowski
      44 mins ago





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

      – Kamil Maciorowski
      44 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
                2 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
                2 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 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
                2 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
                2 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
              2 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
              2 hours 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 47 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

                      名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                      Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

                      Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar