How to get the last occurrence of lines between two patterns from a file?












2














I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns.



The patterns will be along the lines of;



Summary process started at <datestring>


and



Summary process finished at <datestring> with return code <num>


There will be several instances of these patterns throughout the file, along with a lot of other information. I'd like to print the only the last occurrence.



I know that I can use:



sed -n '/StartPattern/,/EndPattern/p' FileName


To get lines between the patterns, but not sure how to get the last instance.
Sed or awk solutions would be fine.



Edit:
I've not been clear at all about the behaviour that I want when multiple StartPatterns appear with no EndPattern, or if there's no EndPattern before the end of file, after detecting a StartPattern



For multiple StartPatterns with missing EndPattern, I'd only like lines from the last StartPattern to the EndPattern.



For a StartPattern which reaches the EOF without an EndPattern, I'd like everything up to the EOF, followed by inputting a string to warn that EOF was reached.










share|improve this question





























    2














    I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns.



    The patterns will be along the lines of;



    Summary process started at <datestring>


    and



    Summary process finished at <datestring> with return code <num>


    There will be several instances of these patterns throughout the file, along with a lot of other information. I'd like to print the only the last occurrence.



    I know that I can use:



    sed -n '/StartPattern/,/EndPattern/p' FileName


    To get lines between the patterns, but not sure how to get the last instance.
    Sed or awk solutions would be fine.



    Edit:
    I've not been clear at all about the behaviour that I want when multiple StartPatterns appear with no EndPattern, or if there's no EndPattern before the end of file, after detecting a StartPattern



    For multiple StartPatterns with missing EndPattern, I'd only like lines from the last StartPattern to the EndPattern.



    For a StartPattern which reaches the EOF without an EndPattern, I'd like everything up to the EOF, followed by inputting a string to warn that EOF was reached.










    share|improve this question



























      2












      2








      2


      1





      I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns.



      The patterns will be along the lines of;



      Summary process started at <datestring>


      and



      Summary process finished at <datestring> with return code <num>


      There will be several instances of these patterns throughout the file, along with a lot of other information. I'd like to print the only the last occurrence.



      I know that I can use:



      sed -n '/StartPattern/,/EndPattern/p' FileName


      To get lines between the patterns, but not sure how to get the last instance.
      Sed or awk solutions would be fine.



      Edit:
      I've not been clear at all about the behaviour that I want when multiple StartPatterns appear with no EndPattern, or if there's no EndPattern before the end of file, after detecting a StartPattern



      For multiple StartPatterns with missing EndPattern, I'd only like lines from the last StartPattern to the EndPattern.



      For a StartPattern which reaches the EOF without an EndPattern, I'd like everything up to the EOF, followed by inputting a string to warn that EOF was reached.










      share|improve this question















      I have a log file which reports on the output of a process, I'd like to extract all lines from between the last occurrence of two patterns.



      The patterns will be along the lines of;



      Summary process started at <datestring>


      and



      Summary process finished at <datestring> with return code <num>


      There will be several instances of these patterns throughout the file, along with a lot of other information. I'd like to print the only the last occurrence.



      I know that I can use:



      sed -n '/StartPattern/,/EndPattern/p' FileName


      To get lines between the patterns, but not sure how to get the last instance.
      Sed or awk solutions would be fine.



      Edit:
      I've not been clear at all about the behaviour that I want when multiple StartPatterns appear with no EndPattern, or if there's no EndPattern before the end of file, after detecting a StartPattern



      For multiple StartPatterns with missing EndPattern, I'd only like lines from the last StartPattern to the EndPattern.



      For a StartPattern which reaches the EOF without an EndPattern, I'd like everything up to the EOF, followed by inputting a string to warn that EOF was reached.







      text-processing awk sed gawk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 14 '16 at 11:35

























      asked Jun 14 '16 at 10:01









      Arronical

      235111




      235111






















          3 Answers
          3






          active

          oldest

          votes


















          4














          You can always do:



          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          If your system doesn't have GNU tac, you may be able to use tail -r instead.



          You can also do it like:



          awk '
          inside {
          text = text $0 RS
          if (/EndPattern/) inside=0
          next
          }
          /StartPattern/ {
          inside = 1
          text = $0 RS
          }
          END {printf "%s", text}' < filename


          But that means reading the whole file.



          Note that it may give different results if there's another StartPattern in between a StartPattern and the next EndPattern or if the last StartPattern does not have an ending EndPattern or if there are lines matching both StartPattern and EndPattern.



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {printf "%s", text}' < filename


          Would make it behave more like the tac+sed+tac approach (except for the unclosed trailing StartPattern case).



          That last one seems to be the closest to your edited requirements. To add the warning would simply be:



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {
          printf "%s", text
          if (inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          }' < filename


          To avoid reading the whole file:



          tac < filename | awk '
          /StartPattern/ {
          printf "%s", $0 RS text
          if (!inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          exit
          }
          /EndPattern/ {inside = 1; text = ""}
          {text = $0 RS text}'


          Portability note: for /dev/stderr, you need either a system with such a special file (beware that on Linux if stderr is open on a seekable file that will write the text at the beginning of the file instead of the current position within the file) or an awk implementation that emulates it like gawk, mawk or busybox awk (those work around the Linux issue mentioned above).



          On other systems, you can replace print ... > "/dev/stderr" with print ... | "cat>&2".






          share|improve this answer























          • But that means reading the whole file. Doesn't tac need to read the whole file?
            – 123
            Jun 14 '16 at 10:48










          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
            – Arronical
            Jun 14 '16 at 11:42










          • @Arronical, see edit.
            – Stéphane Chazelas
            Jun 14 '16 at 12:08



















          3














          You can use GNU sed like so



          sed '/START/{:1;$!{/END/!{N;b1};h}};${x;p};d' file


          Just overwrites the hold space every occurrence of the full multiline pattern. Prints it at the end of the file.



          This will provide consistent behaviour such as




          • Both START and END are on the same line, will match line.

          • Multiple STARTs after the initial START, will match all until END

          • Will not print match if there is no END, will print last occurrence of full START to END






          share|improve this answer























          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
            – Arronical
            Jun 14 '16 at 11:41






          • 1




            (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
            – Stéphane Chazelas
            Jun 14 '16 at 13:33










          • Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
            – 123
            Jun 14 '16 at 13:36



















          -1














          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          This also returning the StartPattern and EndPAttern in the output. Is it possible to just get the text between the patterns.






          share|improve this answer










          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            How does this differ from the accepted answer?
            – Thomas
            55 mins ago











          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f289642%2fhow-to-get-the-last-occurrence-of-lines-between-two-patterns-from-a-file%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You can always do:



          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          If your system doesn't have GNU tac, you may be able to use tail -r instead.



          You can also do it like:



          awk '
          inside {
          text = text $0 RS
          if (/EndPattern/) inside=0
          next
          }
          /StartPattern/ {
          inside = 1
          text = $0 RS
          }
          END {printf "%s", text}' < filename


          But that means reading the whole file.



          Note that it may give different results if there's another StartPattern in between a StartPattern and the next EndPattern or if the last StartPattern does not have an ending EndPattern or if there are lines matching both StartPattern and EndPattern.



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {printf "%s", text}' < filename


          Would make it behave more like the tac+sed+tac approach (except for the unclosed trailing StartPattern case).



          That last one seems to be the closest to your edited requirements. To add the warning would simply be:



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {
          printf "%s", text
          if (inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          }' < filename


          To avoid reading the whole file:



          tac < filename | awk '
          /StartPattern/ {
          printf "%s", $0 RS text
          if (!inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          exit
          }
          /EndPattern/ {inside = 1; text = ""}
          {text = $0 RS text}'


          Portability note: for /dev/stderr, you need either a system with such a special file (beware that on Linux if stderr is open on a seekable file that will write the text at the beginning of the file instead of the current position within the file) or an awk implementation that emulates it like gawk, mawk or busybox awk (those work around the Linux issue mentioned above).



          On other systems, you can replace print ... > "/dev/stderr" with print ... | "cat>&2".






          share|improve this answer























          • But that means reading the whole file. Doesn't tac need to read the whole file?
            – 123
            Jun 14 '16 at 10:48










          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
            – Arronical
            Jun 14 '16 at 11:42










          • @Arronical, see edit.
            – Stéphane Chazelas
            Jun 14 '16 at 12:08
















          4














          You can always do:



          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          If your system doesn't have GNU tac, you may be able to use tail -r instead.



          You can also do it like:



          awk '
          inside {
          text = text $0 RS
          if (/EndPattern/) inside=0
          next
          }
          /StartPattern/ {
          inside = 1
          text = $0 RS
          }
          END {printf "%s", text}' < filename


          But that means reading the whole file.



          Note that it may give different results if there's another StartPattern in between a StartPattern and the next EndPattern or if the last StartPattern does not have an ending EndPattern or if there are lines matching both StartPattern and EndPattern.



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {printf "%s", text}' < filename


          Would make it behave more like the tac+sed+tac approach (except for the unclosed trailing StartPattern case).



          That last one seems to be the closest to your edited requirements. To add the warning would simply be:



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {
          printf "%s", text
          if (inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          }' < filename


          To avoid reading the whole file:



          tac < filename | awk '
          /StartPattern/ {
          printf "%s", $0 RS text
          if (!inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          exit
          }
          /EndPattern/ {inside = 1; text = ""}
          {text = $0 RS text}'


          Portability note: for /dev/stderr, you need either a system with such a special file (beware that on Linux if stderr is open on a seekable file that will write the text at the beginning of the file instead of the current position within the file) or an awk implementation that emulates it like gawk, mawk or busybox awk (those work around the Linux issue mentioned above).



          On other systems, you can replace print ... > "/dev/stderr" with print ... | "cat>&2".






          share|improve this answer























          • But that means reading the whole file. Doesn't tac need to read the whole file?
            – 123
            Jun 14 '16 at 10:48










          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
            – Arronical
            Jun 14 '16 at 11:42










          • @Arronical, see edit.
            – Stéphane Chazelas
            Jun 14 '16 at 12:08














          4












          4








          4






          You can always do:



          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          If your system doesn't have GNU tac, you may be able to use tail -r instead.



          You can also do it like:



          awk '
          inside {
          text = text $0 RS
          if (/EndPattern/) inside=0
          next
          }
          /StartPattern/ {
          inside = 1
          text = $0 RS
          }
          END {printf "%s", text}' < filename


          But that means reading the whole file.



          Note that it may give different results if there's another StartPattern in between a StartPattern and the next EndPattern or if the last StartPattern does not have an ending EndPattern or if there are lines matching both StartPattern and EndPattern.



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {printf "%s", text}' < filename


          Would make it behave more like the tac+sed+tac approach (except for the unclosed trailing StartPattern case).



          That last one seems to be the closest to your edited requirements. To add the warning would simply be:



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {
          printf "%s", text
          if (inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          }' < filename


          To avoid reading the whole file:



          tac < filename | awk '
          /StartPattern/ {
          printf "%s", $0 RS text
          if (!inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          exit
          }
          /EndPattern/ {inside = 1; text = ""}
          {text = $0 RS text}'


          Portability note: for /dev/stderr, you need either a system with such a special file (beware that on Linux if stderr is open on a seekable file that will write the text at the beginning of the file instead of the current position within the file) or an awk implementation that emulates it like gawk, mawk or busybox awk (those work around the Linux issue mentioned above).



          On other systems, you can replace print ... > "/dev/stderr" with print ... | "cat>&2".






          share|improve this answer














          You can always do:



          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          If your system doesn't have GNU tac, you may be able to use tail -r instead.



          You can also do it like:



          awk '
          inside {
          text = text $0 RS
          if (/EndPattern/) inside=0
          next
          }
          /StartPattern/ {
          inside = 1
          text = $0 RS
          }
          END {printf "%s", text}' < filename


          But that means reading the whole file.



          Note that it may give different results if there's another StartPattern in between a StartPattern and the next EndPattern or if the last StartPattern does not have an ending EndPattern or if there are lines matching both StartPattern and EndPattern.



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {printf "%s", text}' < filename


          Would make it behave more like the tac+sed+tac approach (except for the unclosed trailing StartPattern case).



          That last one seems to be the closest to your edited requirements. To add the warning would simply be:



          awk '
          /StartPattern/ {
          inside = 1
          text = ""
          }
          inside {text = text $0 RS}
          /EndPattern/ {inside = 0}
          END {
          printf "%s", text
          if (inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          }' < filename


          To avoid reading the whole file:



          tac < filename | awk '
          /StartPattern/ {
          printf "%s", $0 RS text
          if (!inside)
          print "Warning: EOF reached without seeing the end pattern" > "/dev/stderr"
          exit
          }
          /EndPattern/ {inside = 1; text = ""}
          {text = $0 RS text}'


          Portability note: for /dev/stderr, you need either a system with such a special file (beware that on Linux if stderr is open on a seekable file that will write the text at the beginning of the file instead of the current position within the file) or an awk implementation that emulates it like gawk, mawk or busybox awk (those work around the Linux issue mentioned above).



          On other systems, you can replace print ... > "/dev/stderr" with print ... | "cat>&2".







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 14 '16 at 12:22

























          answered Jun 14 '16 at 10:24









          Stéphane Chazelas

          299k54563913




          299k54563913












          • But that means reading the whole file. Doesn't tac need to read the whole file?
            – 123
            Jun 14 '16 at 10:48










          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
            – Arronical
            Jun 14 '16 at 11:42










          • @Arronical, see edit.
            – Stéphane Chazelas
            Jun 14 '16 at 12:08


















          • But that means reading the whole file. Doesn't tac need to read the whole file?
            – 123
            Jun 14 '16 at 10:48










          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
            – Arronical
            Jun 14 '16 at 11:42










          • @Arronical, see edit.
            – Stéphane Chazelas
            Jun 14 '16 at 12:08
















          But that means reading the whole file. Doesn't tac need to read the whole file?
          – 123
          Jun 14 '16 at 10:48




          But that means reading the whole file. Doesn't tac need to read the whole file?
          – 123
          Jun 14 '16 at 10:48












          Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
          – Arronical
          Jun 14 '16 at 11:42




          Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though. The file is unlikely to be huge, so reading it all won't be a problem.
          – Arronical
          Jun 14 '16 at 11:42












          @Arronical, see edit.
          – Stéphane Chazelas
          Jun 14 '16 at 12:08




          @Arronical, see edit.
          – Stéphane Chazelas
          Jun 14 '16 at 12:08













          3














          You can use GNU sed like so



          sed '/START/{:1;$!{/END/!{N;b1};h}};${x;p};d' file


          Just overwrites the hold space every occurrence of the full multiline pattern. Prints it at the end of the file.



          This will provide consistent behaviour such as




          • Both START and END are on the same line, will match line.

          • Multiple STARTs after the initial START, will match all until END

          • Will not print match if there is no END, will print last occurrence of full START to END






          share|improve this answer























          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
            – Arronical
            Jun 14 '16 at 11:41






          • 1




            (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
            – Stéphane Chazelas
            Jun 14 '16 at 13:33










          • Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
            – 123
            Jun 14 '16 at 13:36
















          3














          You can use GNU sed like so



          sed '/START/{:1;$!{/END/!{N;b1};h}};${x;p};d' file


          Just overwrites the hold space every occurrence of the full multiline pattern. Prints it at the end of the file.



          This will provide consistent behaviour such as




          • Both START and END are on the same line, will match line.

          • Multiple STARTs after the initial START, will match all until END

          • Will not print match if there is no END, will print last occurrence of full START to END






          share|improve this answer























          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
            – Arronical
            Jun 14 '16 at 11:41






          • 1




            (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
            – Stéphane Chazelas
            Jun 14 '16 at 13:33










          • Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
            – 123
            Jun 14 '16 at 13:36














          3












          3








          3






          You can use GNU sed like so



          sed '/START/{:1;$!{/END/!{N;b1};h}};${x;p};d' file


          Just overwrites the hold space every occurrence of the full multiline pattern. Prints it at the end of the file.



          This will provide consistent behaviour such as




          • Both START and END are on the same line, will match line.

          • Multiple STARTs after the initial START, will match all until END

          • Will not print match if there is no END, will print last occurrence of full START to END






          share|improve this answer














          You can use GNU sed like so



          sed '/START/{:1;$!{/END/!{N;b1};h}};${x;p};d' file


          Just overwrites the hold space every occurrence of the full multiline pattern. Prints it at the end of the file.



          This will provide consistent behaviour such as




          • Both START and END are on the same line, will match line.

          • Multiple STARTs after the initial START, will match all until END

          • Will not print match if there is no END, will print last occurrence of full START to END







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 14 '16 at 12:09









          Stéphane Chazelas

          299k54563913




          299k54563913










          answered Jun 14 '16 at 10:52









          123

          1,49738




          1,49738












          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
            – Arronical
            Jun 14 '16 at 11:41






          • 1




            (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
            – Stéphane Chazelas
            Jun 14 '16 at 13:33










          • Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
            – 123
            Jun 14 '16 at 13:36


















          • Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
            – Arronical
            Jun 14 '16 at 11:41






          • 1




            (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
            – Stéphane Chazelas
            Jun 14 '16 at 13:33










          • Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
            – 123
            Jun 14 '16 at 13:36
















          Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
          – Arronical
          Jun 14 '16 at 11:41




          Thanks for the answer and explanation of behaviour. I've realised that my question was vague around expected behaviour in unusual cases, so have edited for clarity. You have my +1 already though.
          – Arronical
          Jun 14 '16 at 11:41




          1




          1




          (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
          – Stéphane Chazelas
          Jun 14 '16 at 13:33




          (note that you can ping editors with @... even if it doesn't offer you completion) GNU specific: } not preceded by ;, labels followed by something, } followed by something. The portable equivalent would be sed -e '/START/{:1' -e '$!{/END/!{N;b1' -e '}' -e 'h;}' -e '}' -e '${x;p;}' -e d (or use separate lines instead of additional -es.
          – Stéphane Chazelas
          Jun 14 '16 at 13:33












          Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
          – 123
          Jun 14 '16 at 13:36




          Ah right, didn't know you can ping editor, thanks. I've deleted my comment from your answer. Thanks for the posix version as well.
          – 123
          Jun 14 '16 at 13:36











          -1














          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          This also returning the StartPattern and EndPAttern in the output. Is it possible to just get the text between the patterns.






          share|improve this answer










          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            How does this differ from the accepted answer?
            – Thomas
            55 mins ago
















          -1














          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          This also returning the StartPattern and EndPAttern in the output. Is it possible to just get the text between the patterns.






          share|improve this answer










          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.














          • 1




            How does this differ from the accepted answer?
            – Thomas
            55 mins ago














          -1












          -1








          -1






          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          This also returning the StartPattern and EndPAttern in the output. Is it possible to just get the text between the patterns.






          share|improve this answer










          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          tac < fileName | sed  '/EndPattern/,$!d;/StartPattern/q' | tac


          This also returning the StartPattern and EndPAttern in the output. Is it possible to just get the text between the patterns.







          share|improve this answer










          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 57 mins ago









          Thomas

          3,71061225




          3,71061225






          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 1 hour ago









          user328837

          1




          1




          New contributor




          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          user328837 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.








          • 1




            How does this differ from the accepted answer?
            – Thomas
            55 mins ago














          • 1




            How does this differ from the accepted answer?
            – Thomas
            55 mins ago








          1




          1




          How does this differ from the accepted answer?
          – Thomas
          55 mins ago




          How does this differ from the accepted answer?
          – Thomas
          55 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2funix.stackexchange.com%2fquestions%2f289642%2fhow-to-get-the-last-occurrence-of-lines-between-two-patterns-from-a-file%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

          サソリ

          広島県道265号伴広島線

          Accessing regular linux commands in Huawei's Dopra Linux