Bash: compare target date to series of dates











up vote
0
down vote

favorite












Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:



target: 2018-12-03 19:09:56.250641



columns:



5346    2018-12-06 17:44:35.010724
6347 2018-12-05 17:50:46.475593
7284 2018-12-04 18:32:11.665405
0298 2018-12-02 22:28:04.59453
1836 2018-12-02 22:27:47.585642
6653 2018-12-02 21:26:13.942103
9274 2018-12-02 21:23:28.318704


I would want to return 0298. What is the cleanest way to do this?










share|improve this question









New contributor




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
























    up vote
    0
    down vote

    favorite












    Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:



    target: 2018-12-03 19:09:56.250641



    columns:



    5346    2018-12-06 17:44:35.010724
    6347 2018-12-05 17:50:46.475593
    7284 2018-12-04 18:32:11.665405
    0298 2018-12-02 22:28:04.59453
    1836 2018-12-02 22:27:47.585642
    6653 2018-12-02 21:26:13.942103
    9274 2018-12-02 21:23:28.318704


    I would want to return 0298. What is the cleanest way to do this?










    share|improve this question









    New contributor




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






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:



      target: 2018-12-03 19:09:56.250641



      columns:



      5346    2018-12-06 17:44:35.010724
      6347 2018-12-05 17:50:46.475593
      7284 2018-12-04 18:32:11.665405
      0298 2018-12-02 22:28:04.59453
      1836 2018-12-02 22:27:47.585642
      6653 2018-12-02 21:26:13.942103
      9274 2018-12-02 21:23:28.318704


      I would want to return 0298. What is the cleanest way to do this?










      share|improve this question









      New contributor




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











      Given a target date and a set of two columns (the first being a random number and the second a date in order from most recent to oldest), I want to find the number for the row with the date closest to but earlier than the target. For example, given the following:



      target: 2018-12-03 19:09:56.250641



      columns:



      5346    2018-12-06 17:44:35.010724
      6347 2018-12-05 17:50:46.475593
      7284 2018-12-04 18:32:11.665405
      0298 2018-12-02 22:28:04.59453
      1836 2018-12-02 22:27:47.585642
      6653 2018-12-02 21:26:13.942103
      9274 2018-12-02 21:23:28.318704


      I would want to return 0298. What is the cleanest way to do this?







      text-processing date






      share|improve this question









      New contributor




      Wmbuch 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




      Wmbuch 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 2 days ago









      Jeff Schaller

      37.5k1052121




      37.5k1052121






      New contributor




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









      asked 2 days ago









      Wmbuch

      31




      31




      New contributor




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





      New contributor





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






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






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).



          There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.



          To solve this issue, set a variable with the value of the search date and use this command:



           s='2018-12-03 19:09:56.250641'

          awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile


          That is:




          • Compare the concatenated values of fields 2 and 3 with the value searched.

          • When this comparison becomes true (lower)

          • Print the first column and exit.






          share|improve this answer





















          • Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
            – Wmbuch
            2 days ago










          • Second ($2) and third ($3) column?.
            – Isaac
            2 days ago










          • Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
            – Wmbuch
            2 days ago


















          up vote
          0
          down vote













          I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:



          $ echo "0000    $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
          0298





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


            }
            });






            Wmbuch 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%2f486717%2fbash-compare-target-date-to-series-of-dates%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
            2
            down vote



            accepted










            Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).



            There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.



            To solve this issue, set a variable with the value of the search date and use this command:



             s='2018-12-03 19:09:56.250641'

            awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile


            That is:




            • Compare the concatenated values of fields 2 and 3 with the value searched.

            • When this comparison becomes true (lower)

            • Print the first column and exit.






            share|improve this answer





















            • Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
              – Wmbuch
              2 days ago










            • Second ($2) and third ($3) column?.
              – Isaac
              2 days ago










            • Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
              – Wmbuch
              2 days ago















            up vote
            2
            down vote



            accepted










            Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).



            There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.



            To solve this issue, set a variable with the value of the search date and use this command:



             s='2018-12-03 19:09:56.250641'

            awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile


            That is:




            • Compare the concatenated values of fields 2 and 3 with the value searched.

            • When this comparison becomes true (lower)

            • Print the first column and exit.






            share|improve this answer





















            • Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
              – Wmbuch
              2 days ago










            • Second ($2) and third ($3) column?.
              – Isaac
              2 days ago










            • Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
              – Wmbuch
              2 days ago













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).



            There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.



            To solve this issue, set a variable with the value of the search date and use this command:



             s='2018-12-03 19:09:56.250641'

            awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile


            That is:




            • Compare the concatenated values of fields 2 and 3 with the value searched.

            • When this comparison becomes true (lower)

            • Print the first column and exit.






            share|improve this answer












            Since the dates are in Y-M-D H:M:S order, they could be compared as text (lexicographical or dictionary order in layman terms).



            There are three fields (columns), not two. Unless the delimiter for the first column is different than the delimiter for the other columns. As you present the delimiters as space there is no way for us to know if it is an space or a tab or something else. I'll assume three columns delimited by either space or tabs.



            To solve this issue, set a variable with the value of the search date and use this command:



             s='2018-12-03 19:09:56.250641'

            awk -vs="$s" '( $2" "$3 < s ){ print $1; exit }' infile


            That is:




            • Compare the concatenated values of fields 2 and 3 with the value searched.

            • When this comparison becomes true (lower)

            • Print the first column and exit.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            Isaac

            10.8k11447




            10.8k11447












            • Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
              – Wmbuch
              2 days ago










            • Second ($2) and third ($3) column?.
              – Isaac
              2 days ago










            • Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
              – Wmbuch
              2 days ago


















            • Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
              – Wmbuch
              2 days ago










            • Second ($2) and third ($3) column?.
              – Isaac
              2 days ago










            • Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
              – Wmbuch
              2 days ago
















            Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
            – Wmbuch
            2 days ago




            Works perfectly! I'm not too familiar with awk syntax. Would you mind explaining what $2" "$3 < s is doing? I get that you're comparing something to the s variable, but I'm unsure what parts of the input are represented by $2 and $3
            – Wmbuch
            2 days ago












            Second ($2) and third ($3) column?.
            – Isaac
            2 days ago




            Second ($2) and third ($3) column?.
            – Isaac
            2 days ago












            Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
            – Wmbuch
            2 days ago




            Oh, I see the problem I was having. I hadn't looked close enough at the time stamps to notice there was a space in them. So when you mentioned a third field, I thought for some weird reason awk treated the whitespace as a "field", which didn't make sense to me and threw my whole interpretation of the syntax into disarray :P
            – Wmbuch
            2 days ago












            up vote
            0
            down vote













            I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:



            $ echo "0000    $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
            0298





            share|improve this answer

























              up vote
              0
              down vote













              I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:



              $ echo "0000    $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
              0298





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:



                $ echo "0000    $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
                0298





                share|improve this answer












                I normally would do this with awk as well, but here's a pipe of text tools to achieve the same result:



                $ echo "0000    $s" | sort -k2 - infile | grep -B1 "$s" | head -1 | cut -d" " -f1
                0298






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                RudiC

                3,7351312




                3,7351312






















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










                    draft saved

                    draft discarded


















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













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












                    Wmbuch 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%2f486717%2fbash-compare-target-date-to-series-of-dates%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

                    Entries order in /etc/network/interfaces

                    新発田市

                    Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)