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...

About the actual radiative impact of greenhouse gas emission over time

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

PTIJ: Who should I vote for? (21st Knesset Edition)

Print a physical multiplication table

If I am holding an item before I cast Blink, will it move with me through the Ethereal Plane?

A single argument pattern definition applies to multiple-argument patterns?

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

Is it good practice to use Linear Least-Squares with SMA?

Is it insecure to send a password in a `curl` command?

Knife as defense against stray dogs

Brexit - No Deal Rejection

This word with a lot of past tenses

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

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

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

Professor being mistaken for a grad student

"of which" is correct here?

What is the purpose or proof behind chain rule?

Math equation in non italic font

Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?

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

How to make healing in an exploration game interesting

Counting models satisfying a boolean formula

Are Roman Catholic priests ever addressed as pastor



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



















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

        – Kamil Maciorowski
        48 mins ago

















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

      – Kamil Maciorowski
      48 mins ago





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

      – Kamil Maciorowski
      48 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,0311913




          1,0311913























              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 52 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

                      Щит и меч (фильм) Содержание Названия серий | Сюжет |...

                      Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

                      Meter-Bus Содержание Параметры шины | Стандартизация |...