Extract a line from a file with sed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












How do I write a sed script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:



[user]$ cat test.txt
start
next
This line should print
Ignore this


[user]$ display.sed test.txt
This line should print

[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too

[user]$ display.sed test1.txt
This line should print






share|improve this question






















  • What if you have next and then something after the second start, should the script print something too or should it print only the 1st occurrence ?
    – don_crissti
    Oct 29 '17 at 23:11











  • It should print only the 1st occurrence after start+next.
    – Hazem Ahmed
    Oct 29 '17 at 23:12














up vote
3
down vote

favorite












How do I write a sed script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:



[user]$ cat test.txt
start
next
This line should print
Ignore this


[user]$ display.sed test.txt
This line should print

[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too

[user]$ display.sed test1.txt
This line should print






share|improve this question






















  • What if you have next and then something after the second start, should the script print something too or should it print only the 1st occurrence ?
    – don_crissti
    Oct 29 '17 at 23:11











  • It should print only the 1st occurrence after start+next.
    – Hazem Ahmed
    Oct 29 '17 at 23:12












up vote
3
down vote

favorite









up vote
3
down vote

favorite











How do I write a sed script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:



[user]$ cat test.txt
start
next
This line should print
Ignore this


[user]$ display.sed test.txt
This line should print

[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too

[user]$ display.sed test1.txt
This line should print






share|improve this question














How do I write a sed script that scans the input file for "start" and finds the line containing "next" and displays the following line? Something like this:



[user]$ cat test.txt
start
next
This line should print
Ignore this


[user]$ display.sed test.txt
This line should print

[user]$ cat test1.txt
Ignore this
next
Ignore this
start
Ignore this
next
This line should print
Ignore this
next
Too late so ignore this too
start
Ignore this too

[user]$ display.sed test1.txt
This line should print








share|improve this question













share|improve this question




share|improve this question








edited Oct 29 '17 at 23:15









don_crissti

46.9k15124154




46.9k15124154










asked Oct 29 '17 at 22:58









Hazem Ahmed

161




161











  • What if you have next and then something after the second start, should the script print something too or should it print only the 1st occurrence ?
    – don_crissti
    Oct 29 '17 at 23:11











  • It should print only the 1st occurrence after start+next.
    – Hazem Ahmed
    Oct 29 '17 at 23:12
















  • What if you have next and then something after the second start, should the script print something too or should it print only the 1st occurrence ?
    – don_crissti
    Oct 29 '17 at 23:11











  • It should print only the 1st occurrence after start+next.
    – Hazem Ahmed
    Oct 29 '17 at 23:12















What if you have next and then something after the second start, should the script print something too or should it print only the 1st occurrence ?
– don_crissti
Oct 29 '17 at 23:11





What if you have next and then something after the second start, should the script print something too or should it print only the 1st occurrence ?
– don_crissti
Oct 29 '17 at 23:11













It should print only the 1st occurrence after start+next.
– Hazem Ahmed
Oct 29 '17 at 23:12




It should print only the 1st occurrence after start+next.
– Hazem Ahmed
Oct 29 '17 at 23:12










3 Answers
3






active

oldest

votes

















up vote
3
down vote













You could use a range (from 1st occurrence of start to end of file) and delete all lines in that range that don't match next. If a line matches, read in the next line, print it then quit:



sed -n '/start/,$
/next/!d;n;p;q
' infile


I guess what you actually want is a file display.sed with the following content:



#!/bin/sed -nf

/start/,$
/next/!d;n;p;q






share|improve this answer






















  • it gives me an unterminated 's' command. Am i doing something wrong?
    – Hazem Ahmed
    Oct 29 '17 at 23:20










  • I wrote the following: #!/bin/sed -f then your code that you just posted.
    – Hazem Ahmed
    Oct 29 '17 at 23:23











  • if the file name is passed as an argument beside the file, how do i access it from the script?
    – Hazem Ahmed
    Oct 29 '17 at 23:29










  • recommendation?
    – Hazem Ahmed
    Oct 29 '17 at 23:38










  • @HazemAhmed - see my edit; btw, is this homework ?
    – don_crissti
    Oct 29 '17 at 23:45

















up vote
2
down vote













Using awk would be more appropriate for such case cause it should work even on non-GNU awk implementations:



awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt


  • /^start/ f=1 - set active flag f=1 on encountering line start


  • f && /^next/ && getline nl>0 - on encountering next line (with previously matched start line - ensured by active f flag) - check if the next needed line exists with getline nl>0


  • nl ("needed line") - contains the line following line next



The output (for your current input content):



This line should print





share|improve this answer





























    up vote
    1
    down vote













    What about grep? I will add explanation, if this command does, what you want.



    grep -Pzo '(?s)start.*?nextnK.*?n' input.txt


    Input (your both examples merged)



    start
    next
    This line should print
    Ignore this
    Ignore this
    next
    Ignore this
    start
    Ignore this
    next
    This line should print
    Ignore this
    next
    Too late so ignore this too
    start
    Ignore this too


    Output



    This line should print
    This line should print





    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401307%2fextract-a-line-from-a-file-with-sed%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      You could use a range (from 1st occurrence of start to end of file) and delete all lines in that range that don't match next. If a line matches, read in the next line, print it then quit:



      sed -n '/start/,$
      /next/!d;n;p;q
      ' infile


      I guess what you actually want is a file display.sed with the following content:



      #!/bin/sed -nf

      /start/,$
      /next/!d;n;p;q






      share|improve this answer






















      • it gives me an unterminated 's' command. Am i doing something wrong?
        – Hazem Ahmed
        Oct 29 '17 at 23:20










      • I wrote the following: #!/bin/sed -f then your code that you just posted.
        – Hazem Ahmed
        Oct 29 '17 at 23:23











      • if the file name is passed as an argument beside the file, how do i access it from the script?
        – Hazem Ahmed
        Oct 29 '17 at 23:29










      • recommendation?
        – Hazem Ahmed
        Oct 29 '17 at 23:38










      • @HazemAhmed - see my edit; btw, is this homework ?
        – don_crissti
        Oct 29 '17 at 23:45














      up vote
      3
      down vote













      You could use a range (from 1st occurrence of start to end of file) and delete all lines in that range that don't match next. If a line matches, read in the next line, print it then quit:



      sed -n '/start/,$
      /next/!d;n;p;q
      ' infile


      I guess what you actually want is a file display.sed with the following content:



      #!/bin/sed -nf

      /start/,$
      /next/!d;n;p;q






      share|improve this answer






















      • it gives me an unterminated 's' command. Am i doing something wrong?
        – Hazem Ahmed
        Oct 29 '17 at 23:20










      • I wrote the following: #!/bin/sed -f then your code that you just posted.
        – Hazem Ahmed
        Oct 29 '17 at 23:23











      • if the file name is passed as an argument beside the file, how do i access it from the script?
        – Hazem Ahmed
        Oct 29 '17 at 23:29










      • recommendation?
        – Hazem Ahmed
        Oct 29 '17 at 23:38










      • @HazemAhmed - see my edit; btw, is this homework ?
        – don_crissti
        Oct 29 '17 at 23:45












      up vote
      3
      down vote










      up vote
      3
      down vote









      You could use a range (from 1st occurrence of start to end of file) and delete all lines in that range that don't match next. If a line matches, read in the next line, print it then quit:



      sed -n '/start/,$
      /next/!d;n;p;q
      ' infile


      I guess what you actually want is a file display.sed with the following content:



      #!/bin/sed -nf

      /start/,$
      /next/!d;n;p;q






      share|improve this answer














      You could use a range (from 1st occurrence of start to end of file) and delete all lines in that range that don't match next. If a line matches, read in the next line, print it then quit:



      sed -n '/start/,$
      /next/!d;n;p;q
      ' infile


      I guess what you actually want is a file display.sed with the following content:



      #!/bin/sed -nf

      /start/,$
      /next/!d;n;p;q







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 29 '17 at 23:45

























      answered Oct 29 '17 at 23:14









      don_crissti

      46.9k15124154




      46.9k15124154











      • it gives me an unterminated 's' command. Am i doing something wrong?
        – Hazem Ahmed
        Oct 29 '17 at 23:20










      • I wrote the following: #!/bin/sed -f then your code that you just posted.
        – Hazem Ahmed
        Oct 29 '17 at 23:23











      • if the file name is passed as an argument beside the file, how do i access it from the script?
        – Hazem Ahmed
        Oct 29 '17 at 23:29










      • recommendation?
        – Hazem Ahmed
        Oct 29 '17 at 23:38










      • @HazemAhmed - see my edit; btw, is this homework ?
        – don_crissti
        Oct 29 '17 at 23:45
















      • it gives me an unterminated 's' command. Am i doing something wrong?
        – Hazem Ahmed
        Oct 29 '17 at 23:20










      • I wrote the following: #!/bin/sed -f then your code that you just posted.
        – Hazem Ahmed
        Oct 29 '17 at 23:23











      • if the file name is passed as an argument beside the file, how do i access it from the script?
        – Hazem Ahmed
        Oct 29 '17 at 23:29










      • recommendation?
        – Hazem Ahmed
        Oct 29 '17 at 23:38










      • @HazemAhmed - see my edit; btw, is this homework ?
        – don_crissti
        Oct 29 '17 at 23:45















      it gives me an unterminated 's' command. Am i doing something wrong?
      – Hazem Ahmed
      Oct 29 '17 at 23:20




      it gives me an unterminated 's' command. Am i doing something wrong?
      – Hazem Ahmed
      Oct 29 '17 at 23:20












      I wrote the following: #!/bin/sed -f then your code that you just posted.
      – Hazem Ahmed
      Oct 29 '17 at 23:23





      I wrote the following: #!/bin/sed -f then your code that you just posted.
      – Hazem Ahmed
      Oct 29 '17 at 23:23













      if the file name is passed as an argument beside the file, how do i access it from the script?
      – Hazem Ahmed
      Oct 29 '17 at 23:29




      if the file name is passed as an argument beside the file, how do i access it from the script?
      – Hazem Ahmed
      Oct 29 '17 at 23:29












      recommendation?
      – Hazem Ahmed
      Oct 29 '17 at 23:38




      recommendation?
      – Hazem Ahmed
      Oct 29 '17 at 23:38












      @HazemAhmed - see my edit; btw, is this homework ?
      – don_crissti
      Oct 29 '17 at 23:45




      @HazemAhmed - see my edit; btw, is this homework ?
      – don_crissti
      Oct 29 '17 at 23:45












      up vote
      2
      down vote













      Using awk would be more appropriate for such case cause it should work even on non-GNU awk implementations:



      awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt


      • /^start/ f=1 - set active flag f=1 on encountering line start


      • f && /^next/ && getline nl>0 - on encountering next line (with previously matched start line - ensured by active f flag) - check if the next needed line exists with getline nl>0


      • nl ("needed line") - contains the line following line next



      The output (for your current input content):



      This line should print





      share|improve this answer


























        up vote
        2
        down vote













        Using awk would be more appropriate for such case cause it should work even on non-GNU awk implementations:



        awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt


        • /^start/ f=1 - set active flag f=1 on encountering line start


        • f && /^next/ && getline nl>0 - on encountering next line (with previously matched start line - ensured by active f flag) - check if the next needed line exists with getline nl>0


        • nl ("needed line") - contains the line following line next



        The output (for your current input content):



        This line should print





        share|improve this answer
























          up vote
          2
          down vote










          up vote
          2
          down vote









          Using awk would be more appropriate for such case cause it should work even on non-GNU awk implementations:



          awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt


          • /^start/ f=1 - set active flag f=1 on encountering line start


          • f && /^next/ && getline nl>0 - on encountering next line (with previously matched start line - ensured by active f flag) - check if the next needed line exists with getline nl>0


          • nl ("needed line") - contains the line following line next



          The output (for your current input content):



          This line should print





          share|improve this answer














          Using awk would be more appropriate for such case cause it should work even on non-GNU awk implementations:



          awk '/^start/ f=1 f && /^next/ && getline nl>0 print nl; exit ' test.txt


          • /^start/ f=1 - set active flag f=1 on encountering line start


          • f && /^next/ && getline nl>0 - on encountering next line (with previously matched start line - ensured by active f flag) - check if the next needed line exists with getline nl>0


          • nl ("needed line") - contains the line following line next



          The output (for your current input content):



          This line should print






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 30 '17 at 12:01

























          answered Oct 29 '17 at 23:15









          RomanPerekhrest

          22.5k12145




          22.5k12145




















              up vote
              1
              down vote













              What about grep? I will add explanation, if this command does, what you want.



              grep -Pzo '(?s)start.*?nextnK.*?n' input.txt


              Input (your both examples merged)



              start
              next
              This line should print
              Ignore this
              Ignore this
              next
              Ignore this
              start
              Ignore this
              next
              This line should print
              Ignore this
              next
              Too late so ignore this too
              start
              Ignore this too


              Output



              This line should print
              This line should print





              share|improve this answer


























                up vote
                1
                down vote













                What about grep? I will add explanation, if this command does, what you want.



                grep -Pzo '(?s)start.*?nextnK.*?n' input.txt


                Input (your both examples merged)



                start
                next
                This line should print
                Ignore this
                Ignore this
                next
                Ignore this
                start
                Ignore this
                next
                This line should print
                Ignore this
                next
                Too late so ignore this too
                start
                Ignore this too


                Output



                This line should print
                This line should print





                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  What about grep? I will add explanation, if this command does, what you want.



                  grep -Pzo '(?s)start.*?nextnK.*?n' input.txt


                  Input (your both examples merged)



                  start
                  next
                  This line should print
                  Ignore this
                  Ignore this
                  next
                  Ignore this
                  start
                  Ignore this
                  next
                  This line should print
                  Ignore this
                  next
                  Too late so ignore this too
                  start
                  Ignore this too


                  Output



                  This line should print
                  This line should print





                  share|improve this answer














                  What about grep? I will add explanation, if this command does, what you want.



                  grep -Pzo '(?s)start.*?nextnK.*?n' input.txt


                  Input (your both examples merged)



                  start
                  next
                  This line should print
                  Ignore this
                  Ignore this
                  next
                  Ignore this
                  start
                  Ignore this
                  next
                  This line should print
                  Ignore this
                  next
                  Too late so ignore this too
                  start
                  Ignore this too


                  Output



                  This line should print
                  This line should print






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 30 '17 at 0:03

























                  answered Oct 29 '17 at 23:53









                  MiniMax

                  2,706719




                  2,706719



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401307%2fextract-a-line-from-a-file-with-sed%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?