Sort lines according to date and time











up vote
2
down vote

favorite
1












I have this file which represents a calender. Each file has the exact date and time, then the name event and a note.



06-12-2016,12:00,gym,leg day
05-04-2018,12:09,gym,hands
09-08-2019,13:08,movies,horror
05-04-2019,14:07,gym,hands
23-03-2018,16:47,dance class
25-04-2019,13:29,dance class
05-12-2017,14:45,songwriting


I need to print the events sorted with the correct order.



I have tried with grep and awk but it didn't work correctly. What can I do?










share|improve this question









New contributor




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
















  • 1




    Can you please show what you tried with grep? What the desired output will be and so on?
    – Valentin Bajrami
    7 hours ago






  • 2




    What is the correct order, by timestamp or by name? If by timestamp, this would have been so much easier if you had only used YYYY-MM-DD dates...
    – Kusalananda
    7 hours ago










  • Try a unix sort sort -n -t- -k 3.1 -k 2.1 -k 1.1 file
    – Dougie
    7 hours ago






  • 3




    @Dougie, please try to restrain yourself from "Answering questions in comments" (as the stock text for comments says). If you have a solution, please consider posting an Answer, instead. Thank you!
    – Jeff Schaller
    6 hours ago






  • 3




    What's an example of an activity that happens before 10 am? Is it 06-12-2016,09:00,teatime or is it 06-12-2016,9:00,teatime?
    – Jeff Schaller
    6 hours ago

















up vote
2
down vote

favorite
1












I have this file which represents a calender. Each file has the exact date and time, then the name event and a note.



06-12-2016,12:00,gym,leg day
05-04-2018,12:09,gym,hands
09-08-2019,13:08,movies,horror
05-04-2019,14:07,gym,hands
23-03-2018,16:47,dance class
25-04-2019,13:29,dance class
05-12-2017,14:45,songwriting


I need to print the events sorted with the correct order.



I have tried with grep and awk but it didn't work correctly. What can I do?










share|improve this question









New contributor




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
















  • 1




    Can you please show what you tried with grep? What the desired output will be and so on?
    – Valentin Bajrami
    7 hours ago






  • 2




    What is the correct order, by timestamp or by name? If by timestamp, this would have been so much easier if you had only used YYYY-MM-DD dates...
    – Kusalananda
    7 hours ago










  • Try a unix sort sort -n -t- -k 3.1 -k 2.1 -k 1.1 file
    – Dougie
    7 hours ago






  • 3




    @Dougie, please try to restrain yourself from "Answering questions in comments" (as the stock text for comments says). If you have a solution, please consider posting an Answer, instead. Thank you!
    – Jeff Schaller
    6 hours ago






  • 3




    What's an example of an activity that happens before 10 am? Is it 06-12-2016,09:00,teatime or is it 06-12-2016,9:00,teatime?
    – Jeff Schaller
    6 hours ago















up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I have this file which represents a calender. Each file has the exact date and time, then the name event and a note.



06-12-2016,12:00,gym,leg day
05-04-2018,12:09,gym,hands
09-08-2019,13:08,movies,horror
05-04-2019,14:07,gym,hands
23-03-2018,16:47,dance class
25-04-2019,13:29,dance class
05-12-2017,14:45,songwriting


I need to print the events sorted with the correct order.



I have tried with grep and awk but it didn't work correctly. What can I do?










share|improve this question









New contributor




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











I have this file which represents a calender. Each file has the exact date and time, then the name event and a note.



06-12-2016,12:00,gym,leg day
05-04-2018,12:09,gym,hands
09-08-2019,13:08,movies,horror
05-04-2019,14:07,gym,hands
23-03-2018,16:47,dance class
25-04-2019,13:29,dance class
05-12-2017,14:45,songwriting


I need to print the events sorted with the correct order.



I have tried with grep and awk but it didn't work correctly. What can I do?







awk grep date sort






share|improve this question









New contributor




user312851 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 question









New contributor




user312851 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 question




share|improve this question








edited 5 hours ago









jimmij

30.6k869103




30.6k869103






New contributor




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









asked 7 hours ago









user312851

112




112




New contributor




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





New contributor





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






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








  • 1




    Can you please show what you tried with grep? What the desired output will be and so on?
    – Valentin Bajrami
    7 hours ago






  • 2




    What is the correct order, by timestamp or by name? If by timestamp, this would have been so much easier if you had only used YYYY-MM-DD dates...
    – Kusalananda
    7 hours ago










  • Try a unix sort sort -n -t- -k 3.1 -k 2.1 -k 1.1 file
    – Dougie
    7 hours ago






  • 3




    @Dougie, please try to restrain yourself from "Answering questions in comments" (as the stock text for comments says). If you have a solution, please consider posting an Answer, instead. Thank you!
    – Jeff Schaller
    6 hours ago






  • 3




    What's an example of an activity that happens before 10 am? Is it 06-12-2016,09:00,teatime or is it 06-12-2016,9:00,teatime?
    – Jeff Schaller
    6 hours ago
















  • 1




    Can you please show what you tried with grep? What the desired output will be and so on?
    – Valentin Bajrami
    7 hours ago






  • 2




    What is the correct order, by timestamp or by name? If by timestamp, this would have been so much easier if you had only used YYYY-MM-DD dates...
    – Kusalananda
    7 hours ago










  • Try a unix sort sort -n -t- -k 3.1 -k 2.1 -k 1.1 file
    – Dougie
    7 hours ago






  • 3




    @Dougie, please try to restrain yourself from "Answering questions in comments" (as the stock text for comments says). If you have a solution, please consider posting an Answer, instead. Thank you!
    – Jeff Schaller
    6 hours ago






  • 3




    What's an example of an activity that happens before 10 am? Is it 06-12-2016,09:00,teatime or is it 06-12-2016,9:00,teatime?
    – Jeff Schaller
    6 hours ago










1




1




Can you please show what you tried with grep? What the desired output will be and so on?
– Valentin Bajrami
7 hours ago




Can you please show what you tried with grep? What the desired output will be and so on?
– Valentin Bajrami
7 hours ago




2




2




What is the correct order, by timestamp or by name? If by timestamp, this would have been so much easier if you had only used YYYY-MM-DD dates...
– Kusalananda
7 hours ago




What is the correct order, by timestamp or by name? If by timestamp, this would have been so much easier if you had only used YYYY-MM-DD dates...
– Kusalananda
7 hours ago












Try a unix sort sort -n -t- -k 3.1 -k 2.1 -k 1.1 file
– Dougie
7 hours ago




Try a unix sort sort -n -t- -k 3.1 -k 2.1 -k 1.1 file
– Dougie
7 hours ago




3




3




@Dougie, please try to restrain yourself from "Answering questions in comments" (as the stock text for comments says). If you have a solution, please consider posting an Answer, instead. Thank you!
– Jeff Schaller
6 hours ago




@Dougie, please try to restrain yourself from "Answering questions in comments" (as the stock text for comments says). If you have a solution, please consider posting an Answer, instead. Thank you!
– Jeff Schaller
6 hours ago




3




3




What's an example of an activity that happens before 10 am? Is it 06-12-2016,09:00,teatime or is it 06-12-2016,9:00,teatime?
– Jeff Schaller
6 hours ago






What's an example of an activity that happens before 10 am? Is it 06-12-2016,09:00,teatime or is it 06-12-2016,9:00,teatime?
– Jeff Schaller
6 hours ago












2 Answers
2






active

oldest

votes

















up vote
4
down vote













I believe the simplest command is



sort -t- -k3.1,3.4 -k2,2 file


This sorts on four characters of the third field and resolve ties by sorting on the second field. If ties are still not solved the order will be determined automatically by comparing all other fields starting from the first one.



Notice that all sorting is done alphabetically because numbers are positive integers so adding additional n doesn't matter.



Test sample:



06-12-2016,12:00,gym,leg day
05-04-2018,12:09,gym,hands
09-08-2019,13:11,movies,horror
09-08-2019,13:09,movies,horror
09-08-2019,13:08,movies,horror
08-08-2019,13:09,movies,horror
05-04-2019,14:07,gym,hands
23-03-2018,16:47,dance class
25-04-2019,13:29,dance class
05-12-2017,14:45,songwriting


Sorted result:



06-12-2016,12:00,gym,leg day
05-12-2017,14:45,songwriting
23-03-2018,16:47,dance class
05-04-2018,12:09,gym,hands
05-04-2019,14:07,gym,hands
25-04-2019,13:29,dance class
08-08-2019,13:09,movies,horror
09-08-2019,13:08,movies,horror
09-08-2019,13:09,movies,horror
09-08-2019,13:11,movies,horror





share|improve this answer




























    up vote
    2
    down vote













    One way would be to manually pick out all of the sort fields:



    sort -t, -k 1.7,1.10n -k 1.4,1.5n -k 1.1,1.2n -k 2.1,2.2n -k 2.4,2.5n input


    This splits the lines up based on comma-separated fields, then uses the positions of the year, month, day, hour, and minutes to sort numerically. Sample output:



    06-12-2016,12:00,gym,leg day
    05-12-2017,14:45,songwriting
    23-03-2018,16:47,dance class
    05-04-2018,12:09,gym,hands
    05-04-2019,14:07,gym,hands
    25-04-2019,13:29,dance class
    09-08-2019,13:08,movies,horror





    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: 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
      });


      }
      });






      user312851 is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487823%2fsort-lines-according-to-date-and-time%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      I believe the simplest command is



      sort -t- -k3.1,3.4 -k2,2 file


      This sorts on four characters of the third field and resolve ties by sorting on the second field. If ties are still not solved the order will be determined automatically by comparing all other fields starting from the first one.



      Notice that all sorting is done alphabetically because numbers are positive integers so adding additional n doesn't matter.



      Test sample:



      06-12-2016,12:00,gym,leg day
      05-04-2018,12:09,gym,hands
      09-08-2019,13:11,movies,horror
      09-08-2019,13:09,movies,horror
      09-08-2019,13:08,movies,horror
      08-08-2019,13:09,movies,horror
      05-04-2019,14:07,gym,hands
      23-03-2018,16:47,dance class
      25-04-2019,13:29,dance class
      05-12-2017,14:45,songwriting


      Sorted result:



      06-12-2016,12:00,gym,leg day
      05-12-2017,14:45,songwriting
      23-03-2018,16:47,dance class
      05-04-2018,12:09,gym,hands
      05-04-2019,14:07,gym,hands
      25-04-2019,13:29,dance class
      08-08-2019,13:09,movies,horror
      09-08-2019,13:08,movies,horror
      09-08-2019,13:09,movies,horror
      09-08-2019,13:11,movies,horror





      share|improve this answer

























        up vote
        4
        down vote













        I believe the simplest command is



        sort -t- -k3.1,3.4 -k2,2 file


        This sorts on four characters of the third field and resolve ties by sorting on the second field. If ties are still not solved the order will be determined automatically by comparing all other fields starting from the first one.



        Notice that all sorting is done alphabetically because numbers are positive integers so adding additional n doesn't matter.



        Test sample:



        06-12-2016,12:00,gym,leg day
        05-04-2018,12:09,gym,hands
        09-08-2019,13:11,movies,horror
        09-08-2019,13:09,movies,horror
        09-08-2019,13:08,movies,horror
        08-08-2019,13:09,movies,horror
        05-04-2019,14:07,gym,hands
        23-03-2018,16:47,dance class
        25-04-2019,13:29,dance class
        05-12-2017,14:45,songwriting


        Sorted result:



        06-12-2016,12:00,gym,leg day
        05-12-2017,14:45,songwriting
        23-03-2018,16:47,dance class
        05-04-2018,12:09,gym,hands
        05-04-2019,14:07,gym,hands
        25-04-2019,13:29,dance class
        08-08-2019,13:09,movies,horror
        09-08-2019,13:08,movies,horror
        09-08-2019,13:09,movies,horror
        09-08-2019,13:11,movies,horror





        share|improve this answer























          up vote
          4
          down vote










          up vote
          4
          down vote









          I believe the simplest command is



          sort -t- -k3.1,3.4 -k2,2 file


          This sorts on four characters of the third field and resolve ties by sorting on the second field. If ties are still not solved the order will be determined automatically by comparing all other fields starting from the first one.



          Notice that all sorting is done alphabetically because numbers are positive integers so adding additional n doesn't matter.



          Test sample:



          06-12-2016,12:00,gym,leg day
          05-04-2018,12:09,gym,hands
          09-08-2019,13:11,movies,horror
          09-08-2019,13:09,movies,horror
          09-08-2019,13:08,movies,horror
          08-08-2019,13:09,movies,horror
          05-04-2019,14:07,gym,hands
          23-03-2018,16:47,dance class
          25-04-2019,13:29,dance class
          05-12-2017,14:45,songwriting


          Sorted result:



          06-12-2016,12:00,gym,leg day
          05-12-2017,14:45,songwriting
          23-03-2018,16:47,dance class
          05-04-2018,12:09,gym,hands
          05-04-2019,14:07,gym,hands
          25-04-2019,13:29,dance class
          08-08-2019,13:09,movies,horror
          09-08-2019,13:08,movies,horror
          09-08-2019,13:09,movies,horror
          09-08-2019,13:11,movies,horror





          share|improve this answer












          I believe the simplest command is



          sort -t- -k3.1,3.4 -k2,2 file


          This sorts on four characters of the third field and resolve ties by sorting on the second field. If ties are still not solved the order will be determined automatically by comparing all other fields starting from the first one.



          Notice that all sorting is done alphabetically because numbers are positive integers so adding additional n doesn't matter.



          Test sample:



          06-12-2016,12:00,gym,leg day
          05-04-2018,12:09,gym,hands
          09-08-2019,13:11,movies,horror
          09-08-2019,13:09,movies,horror
          09-08-2019,13:08,movies,horror
          08-08-2019,13:09,movies,horror
          05-04-2019,14:07,gym,hands
          23-03-2018,16:47,dance class
          25-04-2019,13:29,dance class
          05-12-2017,14:45,songwriting


          Sorted result:



          06-12-2016,12:00,gym,leg day
          05-12-2017,14:45,songwriting
          23-03-2018,16:47,dance class
          05-04-2018,12:09,gym,hands
          05-04-2019,14:07,gym,hands
          25-04-2019,13:29,dance class
          08-08-2019,13:09,movies,horror
          09-08-2019,13:08,movies,horror
          09-08-2019,13:09,movies,horror
          09-08-2019,13:11,movies,horror






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          jimmij

          30.6k869103




          30.6k869103
























              up vote
              2
              down vote













              One way would be to manually pick out all of the sort fields:



              sort -t, -k 1.7,1.10n -k 1.4,1.5n -k 1.1,1.2n -k 2.1,2.2n -k 2.4,2.5n input


              This splits the lines up based on comma-separated fields, then uses the positions of the year, month, day, hour, and minutes to sort numerically. Sample output:



              06-12-2016,12:00,gym,leg day
              05-12-2017,14:45,songwriting
              23-03-2018,16:47,dance class
              05-04-2018,12:09,gym,hands
              05-04-2019,14:07,gym,hands
              25-04-2019,13:29,dance class
              09-08-2019,13:08,movies,horror





              share|improve this answer

























                up vote
                2
                down vote













                One way would be to manually pick out all of the sort fields:



                sort -t, -k 1.7,1.10n -k 1.4,1.5n -k 1.1,1.2n -k 2.1,2.2n -k 2.4,2.5n input


                This splits the lines up based on comma-separated fields, then uses the positions of the year, month, day, hour, and minutes to sort numerically. Sample output:



                06-12-2016,12:00,gym,leg day
                05-12-2017,14:45,songwriting
                23-03-2018,16:47,dance class
                05-04-2018,12:09,gym,hands
                05-04-2019,14:07,gym,hands
                25-04-2019,13:29,dance class
                09-08-2019,13:08,movies,horror





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  One way would be to manually pick out all of the sort fields:



                  sort -t, -k 1.7,1.10n -k 1.4,1.5n -k 1.1,1.2n -k 2.1,2.2n -k 2.4,2.5n input


                  This splits the lines up based on comma-separated fields, then uses the positions of the year, month, day, hour, and minutes to sort numerically. Sample output:



                  06-12-2016,12:00,gym,leg day
                  05-12-2017,14:45,songwriting
                  23-03-2018,16:47,dance class
                  05-04-2018,12:09,gym,hands
                  05-04-2019,14:07,gym,hands
                  25-04-2019,13:29,dance class
                  09-08-2019,13:08,movies,horror





                  share|improve this answer












                  One way would be to manually pick out all of the sort fields:



                  sort -t, -k 1.7,1.10n -k 1.4,1.5n -k 1.1,1.2n -k 2.1,2.2n -k 2.4,2.5n input


                  This splits the lines up based on comma-separated fields, then uses the positions of the year, month, day, hour, and minutes to sort numerically. Sample output:



                  06-12-2016,12:00,gym,leg day
                  05-12-2017,14:45,songwriting
                  23-03-2018,16:47,dance class
                  05-04-2018,12:09,gym,hands
                  05-04-2019,14:07,gym,hands
                  25-04-2019,13:29,dance class
                  09-08-2019,13:08,movies,horror






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 6 hours ago









                  Jeff Schaller

                  37.8k1053122




                  37.8k1053122






















                      user312851 is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      user312851 is a new contributor. Be nice, and check out our Code of Conduct.













                      user312851 is a new contributor. Be nice, and check out our Code of Conduct.












                      user312851 is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f487823%2fsort-lines-according-to-date-and-time%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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)