Calculate or sum timestamps between lines in awk

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












0















I have the following input:



1. Track 01 05:21
2. Track 02 07:39
3. Track 03 04:27


I came up with the following script



awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file


But the result I get is this:



0:01:21
0:04:00
0:07:27


In turn, with the same script, when I have a file with only the time and nothing else I get the correct output



Input:



05:21
07:39
04:27


Output:



0:05:21
0:13:00
0:17:27









share|improve this question


























    0















    I have the following input:



    1. Track 01 05:21
    2. Track 02 07:39
    3. Track 03 04:27


    I came up with the following script



    awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file


    But the result I get is this:



    0:01:21
    0:04:00
    0:07:27


    In turn, with the same script, when I have a file with only the time and nothing else I get the correct output



    Input:



    05:21
    07:39
    04:27


    Output:



    0:05:21
    0:13:00
    0:17:27









    share|improve this question
























      0












      0








      0








      I have the following input:



      1. Track 01 05:21
      2. Track 02 07:39
      3. Track 03 04:27


      I came up with the following script



      awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file


      But the result I get is this:



      0:01:21
      0:04:00
      0:07:27


      In turn, with the same script, when I have a file with only the time and nothing else I get the correct output



      Input:



      05:21
      07:39
      04:27


      Output:



      0:05:21
      0:13:00
      0:17:27









      share|improve this question














      I have the following input:



      1. Track 01 05:21
      2. Track 02 07:39
      3. Track 03 04:27


      I came up with the following script



      awk -F: 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60' file


      But the result I get is this:



      0:01:21
      0:04:00
      0:07:27


      In turn, with the same script, when I have a file with only the time and nothing else I get the correct output



      Input:



      05:21
      07:39
      04:27


      Output:



      0:05:21
      0:13:00
      0:17:27






      awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 6 at 14:54









      nylnyl

      112




      112




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Use two awk commands



          awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'


          the first awk extract the las field of the file and the second awk makes the procees you want.



          this works whether the file contets only the time or not, as long as the time is the last field in the line






          share|improve this answer






























            0














            This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.



            I would do



            awk '
            BEGIN h = m = s = 0
            function add_time(min, sec)
            s += sec
            m += min
            while (s >= 60) s -= 60; m++
            while (m >= 60) m -= 60; h++


            split($NF, t, /:/)
            add_time(t[1], t[2])
            printf "%d:%02d:%02dn", h, m, s

            ' file





            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',
              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%2f504730%2fcalculate-or-sum-timestamps-between-lines-in-awk%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









              0














              Use two awk commands



              awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'


              the first awk extract the las field of the file and the second awk makes the procees you want.



              this works whether the file contets only the time or not, as long as the time is the last field in the line






              share|improve this answer



























                0














                Use two awk commands



                awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'


                the first awk extract the las field of the file and the second awk makes the procees you want.



                this works whether the file contets only the time or not, as long as the time is the last field in the line






                share|improve this answer

























                  0












                  0








                  0







                  Use two awk commands



                  awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'


                  the first awk extract the las field of the file and the second awk makes the procees you want.



                  this works whether the file contets only the time or not, as long as the time is the last field in the line






                  share|improve this answer













                  Use two awk commands



                  awk 'print $NF' file |awk 'a+=$(NF-1)*60+$(NF);printf"%d:%02d:%02dn",a/3600,a%3600/60,a%3600%60'


                  the first awk extract the las field of the file and the second awk makes the procees you want.



                  this works whether the file contets only the time or not, as long as the time is the last field in the line







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 6 at 15:16









                  Emilio GalarragaEmilio Galarraga

                  554410




                  554410























                      0














                      This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.



                      I would do



                      awk '
                      BEGIN h = m = s = 0
                      function add_time(min, sec)
                      s += sec
                      m += min
                      while (s >= 60) s -= 60; m++
                      while (m >= 60) m -= 60; h++


                      split($NF, t, /:/)
                      add_time(t[1], t[2])
                      printf "%d:%02d:%02dn", h, m, s

                      ' file





                      share|improve this answer





























                        0














                        This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.



                        I would do



                        awk '
                        BEGIN h = m = s = 0
                        function add_time(min, sec)
                        s += sec
                        m += min
                        while (s >= 60) s -= 60; m++
                        while (m >= 60) m -= 60; h++


                        split($NF, t, /:/)
                        add_time(t[1], t[2])
                        printf "%d:%02d:%02dn", h, m, s

                        ' file





                        share|improve this answer



























                          0












                          0








                          0







                          This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.



                          I would do



                          awk '
                          BEGIN h = m = s = 0
                          function add_time(min, sec)
                          s += sec
                          m += min
                          while (s >= 60) s -= 60; m++
                          while (m >= 60) m -= 60; h++


                          split($NF, t, /:/)
                          add_time(t[1], t[2])
                          printf "%d:%02d:%02dn", h, m, s

                          ' file





                          share|improve this answer















                          This is the problem you're having: when FS=":", your first field is "1. Track 01 05", and when you use that string in an arithmetic context, the first non-digit truncates the number, so the minute value is "1". You need to use space or colon as the field separator, but then any trailing whitespace means you have extra empty fields.



                          I would do



                          awk '
                          BEGIN h = m = s = 0
                          function add_time(min, sec)
                          s += sec
                          m += min
                          while (s >= 60) s -= 60; m++
                          while (m >= 60) m -= 60; h++


                          split($NF, t, /:/)
                          add_time(t[1], t[2])
                          printf "%d:%02d:%02dn", h, m, s

                          ' file






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 6 at 15:23

























                          answered Mar 6 at 15:17









                          glenn jackmanglenn jackman

                          53k573114




                          53k573114



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504730%2fcalculate-or-sum-timestamps-between-lines-in-awk%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?