Calculate difference in numbers and save in colon-separated time format with bash script2019 Community Moderator ElectionHow to get elapsed time from two “text based” datesProcess Files to create a new fileConcatenate multiple fields separately based on one (key) columnRemoving numeric values in certain columns whilst keeping minus signs?Extract single information from a column in a fileRemove comma from columns in a file if it is at the endarithmetic operations within column with awk or sedArithmetic calculation row-wise for non-fixed column numberHow to process a column's multiple stringsHow to start a new column after every nth row?

PTIJ: Which Dr. Seuss books should one obtain?

Limit max CPU usage SQL SERVER with WSRM

How to test the sharpness of a knife?

Deciphering cause of death?

El Dorado Word Puzzle II: Videogame Edition

Do people actually use the word "kaputt" in conversation?

Showing mass murder in a kid's book

Did I make a mistake by ccing email to boss to others?

Is there a distance limit for minecart tracks?

Can you identify this lizard-like creature I observed in the UK?

Can I cause damage to electrical appliances by unplugging them when they are turned on?

Ways of geometrical multiplication

How to make a list of partial sums using forEach

If Captain Marvel (MCU) were to have a child with a human male, would the child be human or Kree?

Air travel with refrigerated insulin

What is the meaning of "You've never met a graph you didn't like?"

Why didn't Voldemort know what Grindelwald looked like?

How do I Interface a PS/2 Keyboard without Modern Techniques?

What does "tick" mean in this sentence?

Can I run 125kHz RF circuit on a breadboard?

How to make money from a browser who sees 5 seconds into the future of any web page?

When and why was runway 07/25 at Kai Tak removed?

How to leave product feedback on macOS?

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?



Calculate difference in numbers and save in colon-separated time format with bash script



2019 Community Moderator ElectionHow to get elapsed time from two “text based” datesProcess Files to create a new fileConcatenate multiple fields separately based on one (key) columnRemoving numeric values in certain columns whilst keeping minus signs?Extract single information from a column in a fileRemove comma from columns in a file if it is at the endarithmetic operations within column with awk or sedArithmetic calculation row-wise for non-fixed column numberHow to process a column's multiple stringsHow to start a new column after every nth row?










1















I have a tab-separated file, e.g.,



0018803 01 1710 2050 002571 
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971


The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.



I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.



Column 6 result in this sample would be 3:40, 5:51, 00:11, 1:59, 2:05.










share|improve this question









New contributor




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















  • 1





    What unit is are the numbers in column 3 and 4 using?

    – Kusalananda
    19 hours ago











  • @RoVo Well, in that case lines 2, 3, 4 and 5 are weird.

    – Kusalananda
    19 hours ago












  • there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4

    – Aditya
    19 hours ago






  • 1





    If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600

    – Aditya
    19 hours ago






  • 1





    are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123

    – Dalvenjia
    11 hours ago















1















I have a tab-separated file, e.g.,



0018803 01 1710 2050 002571 
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971


The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.



I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.



Column 6 result in this sample would be 3:40, 5:51, 00:11, 1:59, 2:05.










share|improve this question









New contributor




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















  • 1





    What unit is are the numbers in column 3 and 4 using?

    – Kusalananda
    19 hours ago











  • @RoVo Well, in that case lines 2, 3, 4 and 5 are weird.

    – Kusalananda
    19 hours ago












  • there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4

    – Aditya
    19 hours ago






  • 1





    If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600

    – Aditya
    19 hours ago






  • 1





    are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123

    – Dalvenjia
    11 hours ago













1












1








1


1






I have a tab-separated file, e.g.,



0018803 01 1710 2050 002571 
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971


The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.



I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.



Column 6 result in this sample would be 3:40, 5:51, 00:11, 1:59, 2:05.










share|improve this question









New contributor




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












I have a tab-separated file, e.g.,



0018803 01 1710 2050 002571 
0018951 01 1934 2525 003277
0019362 02 2404 2415 002829
0019392 01 2621 2820 001924
0019542 01 2208 2413 003434
0019583 01 1815 2134 002971


The 3rd and 4th columns represent Start Time and End Time in HHMM format. If the Start Time or End Time passes midnight, it keeps incrementing the hours.



I want column 6 to show elapsed time of (Column 4 minus Column 3) in hours and minutes.



Column 6 result in this sample would be 3:40, 5:51, 00:11, 1:59, 2:05.







shell-script text-processing date






share|improve this question









New contributor




Aditya 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




Aditya 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 9 hours ago









Jeff Schaller

43.8k1161141




43.8k1161141






New contributor




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









asked 20 hours ago









AdityaAditya

63




63




New contributor




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





New contributor





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






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







  • 1





    What unit is are the numbers in column 3 and 4 using?

    – Kusalananda
    19 hours ago











  • @RoVo Well, in that case lines 2, 3, 4 and 5 are weird.

    – Kusalananda
    19 hours ago












  • there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4

    – Aditya
    19 hours ago






  • 1





    If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600

    – Aditya
    19 hours ago






  • 1





    are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123

    – Dalvenjia
    11 hours ago












  • 1





    What unit is are the numbers in column 3 and 4 using?

    – Kusalananda
    19 hours ago











  • @RoVo Well, in that case lines 2, 3, 4 and 5 are weird.

    – Kusalananda
    19 hours ago












  • there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4

    – Aditya
    19 hours ago






  • 1





    If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600

    – Aditya
    19 hours ago






  • 1





    are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123

    – Dalvenjia
    11 hours ago







1




1





What unit is are the numbers in column 3 and 4 using?

– Kusalananda
19 hours ago





What unit is are the numbers in column 3 and 4 using?

– Kusalananda
19 hours ago













@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.

– Kusalananda
19 hours ago






@RoVo Well, in that case lines 2, 3, 4 and 5 are weird.

– Kusalananda
19 hours ago














there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4

– Aditya
19 hours ago





there is one java program through that this time is converted to this format.. there is no particular unit defined for col3 and col4

– Aditya
19 hours ago




1




1





If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600

– Aditya
19 hours ago





If a person used to active on internet from 2300 hr to next day 200hour. it will reflect as 2600

– Aditya
19 hours ago




1




1





are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123

– Dalvenjia
11 hours ago





are columns 3 and 4 guaranteed to be 4 characters long? 123 -> 0123

– Dalvenjia
11 hours ago










1 Answer
1






active

oldest

votes


















0














Hi this assumes the start time and end time will always be 4 characters long:



awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'


Explanation:



NF: Number of Fields, this is to skip empty lines



hrs = substr($4, 1, 2) - substr($3, 1, 2);: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs;



min = substr($4, 3, 4) - substr($3, 3, 4);: Repeat the last logic with the last 2 characters for the minutes;



if(min<0) hrs--; min = 60 + min;: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min;



printf "%st%02d:%02dn", $0, hrs, min: Lastly print all the line and the variables hrs and min zero padded






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



    );






    Aditya 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%2f507376%2fcalculate-difference-in-numbers-and-save-in-colon-separated-time-format-with-bas%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Hi this assumes the start time and end time will always be 4 characters long:



    awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'


    Explanation:



    NF: Number of Fields, this is to skip empty lines



    hrs = substr($4, 1, 2) - substr($3, 1, 2);: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs;



    min = substr($4, 3, 4) - substr($3, 3, 4);: Repeat the last logic with the last 2 characters for the minutes;



    if(min<0) hrs--; min = 60 + min;: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min;



    printf "%st%02d:%02dn", $0, hrs, min: Lastly print all the line and the variables hrs and min zero padded






    share|improve this answer





























      0














      Hi this assumes the start time and end time will always be 4 characters long:



      awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'


      Explanation:



      NF: Number of Fields, this is to skip empty lines



      hrs = substr($4, 1, 2) - substr($3, 1, 2);: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs;



      min = substr($4, 3, 4) - substr($3, 3, 4);: Repeat the last logic with the last 2 characters for the minutes;



      if(min<0) hrs--; min = 60 + min;: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min;



      printf "%st%02d:%02dn", $0, hrs, min: Lastly print all the line and the variables hrs and min zero padded






      share|improve this answer



























        0












        0








        0







        Hi this assumes the start time and end time will always be 4 characters long:



        awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'


        Explanation:



        NF: Number of Fields, this is to skip empty lines



        hrs = substr($4, 1, 2) - substr($3, 1, 2);: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs;



        min = substr($4, 3, 4) - substr($3, 3, 4);: Repeat the last logic with the last 2 characters for the minutes;



        if(min<0) hrs--; min = 60 + min;: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min;



        printf "%st%02d:%02dn", $0, hrs, min: Lastly print all the line and the variables hrs and min zero padded






        share|improve this answer















        Hi this assumes the start time and end time will always be 4 characters long:



        awk 'NF hrs = substr($4, 1, 2) - substr($3, 1, 2); min = substr($4, 3, 4) - substr($3, 3, 4); if(min<0) hrs--; min = 60 + min; printf "%st%02d:%02dn", $0, hrs, min'


        Explanation:



        NF: Number of Fields, this is to skip empty lines



        hrs = substr($4, 1, 2) - substr($3, 1, 2);: Get the first 2 characters of column 4 and subtract the first 2 characters of column 3 and assign to variable hrs;



        min = substr($4, 3, 4) - substr($3, 3, 4);: Repeat the last logic with the last 2 characters for the minutes;



        if(min<0) hrs--; min = 60 + min;: If minutes are negative, subtract 1 from hours and assign the result of adding 60 to that negative number to variable min;



        printf "%st%02d:%02dn", $0, hrs, min: Lastly print all the line and the variables hrs and min zero padded







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 10 hours ago

























        answered 10 hours ago









        DalvenjiaDalvenjia

        1,22658




        1,22658




















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









            draft saved

            draft discarded


















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












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











            Aditya 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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507376%2fcalculate-difference-in-numbers-and-save-in-colon-separated-time-format-with-bas%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

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.