Sed range problem if the last pattern is not met

Multi tool use
Multi tool use

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











up vote
0
down vote

favorite












I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00 to 2016-09-29 01:30. That is why I have been using the following command,



$ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'


But problem is if 1:30 is not available in log then it returns all the logs to the end.



So how can work with this so that if 1:30 doesn't exist it will go to the just next record not till end.



Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.










share|improve this question



























    up vote
    0
    down vote

    favorite












    I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00 to 2016-09-29 01:30. That is why I have been using the following command,



    $ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'


    But problem is if 1:30 is not available in log then it returns all the logs to the end.



    So how can work with this so that if 1:30 doesn't exist it will go to the just next record not till end.



    Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00 to 2016-09-29 01:30. That is why I have been using the following command,



      $ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'


      But problem is if 1:30 is not available in log then it returns all the logs to the end.



      So how can work with this so that if 1:30 doesn't exist it will go to the just next record not till end.



      Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.










      share|improve this question















      I am doing a range search with sed. I want to parse the log data from the date and time 2016-09-29 01:00 to 2016-09-29 01:30. That is why I have been using the following command,



      $ sed -n '/2016-09-29 01:/,/2016-09-29 01:30:.*$/p'


      But problem is if 1:30 is not available in log then it returns all the logs to the end.



      So how can work with this so that if 1:30 doesn't exist it will go to the just next record not till end.



      Things to consider: Logs contains stack trace so lines contain stack trace doesn't start with the date.







      sed regular-expression date






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 29 at 11:23

























      asked Aug 29 at 11:03









      muhammad

      499514




      499514




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Not so strange. sed is a stream editor, it processes lines as they come. A range like /a/,/b/ means the lines are selected as soon as a is found, and no-longer selected after b is found. If b is never found, it never stops selecting lines.



          Here, you should rather use awk instead. Assuming those timestamps are at the beginning of the line:



          awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'


          Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.



          Another approach to work around that would be:



          awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
          $0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'


          That is use a range like in sed, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.






          share|improve this answer






















          • Worked like charm. But then I saw that it doesn't include stack traces.
            – muhammad
            Aug 29 at 11:22










          • @muhammad, see edit.
            – Stéphane Chazelas
            Aug 29 at 11:34










          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%2f465477%2fsed-range-problem-if-the-last-pattern-is-not-met%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          Not so strange. sed is a stream editor, it processes lines as they come. A range like /a/,/b/ means the lines are selected as soon as a is found, and no-longer selected after b is found. If b is never found, it never stops selecting lines.



          Here, you should rather use awk instead. Assuming those timestamps are at the beginning of the line:



          awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'


          Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.



          Another approach to work around that would be:



          awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
          $0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'


          That is use a range like in sed, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.






          share|improve this answer






















          • Worked like charm. But then I saw that it doesn't include stack traces.
            – muhammad
            Aug 29 at 11:22










          • @muhammad, see edit.
            – Stéphane Chazelas
            Aug 29 at 11:34














          up vote
          2
          down vote



          accepted










          Not so strange. sed is a stream editor, it processes lines as they come. A range like /a/,/b/ means the lines are selected as soon as a is found, and no-longer selected after b is found. If b is never found, it never stops selecting lines.



          Here, you should rather use awk instead. Assuming those timestamps are at the beginning of the line:



          awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'


          Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.



          Another approach to work around that would be:



          awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
          $0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'


          That is use a range like in sed, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.






          share|improve this answer






















          • Worked like charm. But then I saw that it doesn't include stack traces.
            – muhammad
            Aug 29 at 11:22










          • @muhammad, see edit.
            – Stéphane Chazelas
            Aug 29 at 11:34












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Not so strange. sed is a stream editor, it processes lines as they come. A range like /a/,/b/ means the lines are selected as soon as a is found, and no-longer selected after b is found. If b is never found, it never stops selecting lines.



          Here, you should rather use awk instead. Assuming those timestamps are at the beginning of the line:



          awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'


          Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.



          Another approach to work around that would be:



          awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
          $0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'


          That is use a range like in sed, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.






          share|improve this answer














          Not so strange. sed is a stream editor, it processes lines as they come. A range like /a/,/b/ means the lines are selected as soon as a is found, and no-longer selected after b is found. If b is never found, it never stops selecting lines.



          Here, you should rather use awk instead. Assuming those timestamps are at the beginning of the line:



          awk '$0 >= "2016-09-29 01:" && $0 < "2016-09-29 01:30"'


          Note that it will only select lines that have a timestamp in the range, so would excludes lines that don't have a timestamp even if they are between lines that have timestamps in the range.



          Another approach to work around that would be:



          awk -v start='2016-09-29 01:' -v end='2016-09-29 01:30' '
          $0 >= start && $0 <= end, /^[0-9]4([ :-][0-9]2)5/ && $0 >= end'


          That is use a range like in sed, but enter the range on the first line that is between the 2 dates, and leave it only when we find a line with a timestamp that is greater than the end date.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 29 at 12:05

























          answered Aug 29 at 11:19









          Stéphane Chazelas

          286k53527866




          286k53527866











          • Worked like charm. But then I saw that it doesn't include stack traces.
            – muhammad
            Aug 29 at 11:22










          • @muhammad, see edit.
            – Stéphane Chazelas
            Aug 29 at 11:34
















          • Worked like charm. But then I saw that it doesn't include stack traces.
            – muhammad
            Aug 29 at 11:22










          • @muhammad, see edit.
            – Stéphane Chazelas
            Aug 29 at 11:34















          Worked like charm. But then I saw that it doesn't include stack traces.
          – muhammad
          Aug 29 at 11:22




          Worked like charm. But then I saw that it doesn't include stack traces.
          – muhammad
          Aug 29 at 11:22












          @muhammad, see edit.
          – Stéphane Chazelas
          Aug 29 at 11:34




          @muhammad, see edit.
          – Stéphane Chazelas
          Aug 29 at 11:34

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465477%2fsed-range-problem-if-the-last-pattern-is-not-met%23new-answer', 'question_page');

          );

          Post as a guest













































































          hHI26IGggdwTvMkS,K4 8,bIqodR u,k6B,cLZty8qkO,DM2IL6h3K058zo
          TMB tfd6kL,rj 5qhgDfY

          Popular posts from this blog

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

          How many registers does an x86_64 CPU actually have?

          Displaying single band from multi-band raster using QGIS