Bash: compare target date to series of dates

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











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



























    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

























      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















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 8 at 12:53









      Jeff Schaller

      38k1053123




      38k1053123










      asked Dec 8 at 4:07









      Wmbuch

      1031




      1031




















          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
            Dec 8 at 13:42










          • Second ($2) and third ($3) column?.
            – Isaac
            Dec 8 at 19:52










          • 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
            Dec 8 at 20:18

















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



            );













            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
              Dec 8 at 13:42










            • Second ($2) and third ($3) column?.
              – Isaac
              Dec 8 at 19:52










            • 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
              Dec 8 at 20:18














            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
              Dec 8 at 13:42










            • Second ($2) and third ($3) column?.
              – Isaac
              Dec 8 at 19:52










            • 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
              Dec 8 at 20:18












            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 Dec 8 at 5:41









            Isaac

            11k11648




            11k11648











            • 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
              Dec 8 at 13:42










            • Second ($2) and third ($3) column?.
              – Isaac
              Dec 8 at 19:52










            • 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
              Dec 8 at 20:18
















            • 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
              Dec 8 at 13:42










            • Second ($2) and third ($3) column?.
              – Isaac
              Dec 8 at 19:52










            • 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
              Dec 8 at 20:18















            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
            Dec 8 at 13:42




            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
            Dec 8 at 13:42












            Second ($2) and third ($3) column?.
            – Isaac
            Dec 8 at 19:52




            Second ($2) and third ($3) column?.
            – Isaac
            Dec 8 at 19:52












            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
            Dec 8 at 20:18




            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
            Dec 8 at 20:18












            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 Dec 8 at 12:56









                RudiC

                3,9541312




                3,9541312



























                    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%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

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

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?