Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:25:26+00:00 2026-05-28T13:25:26+00:00

I’m trying to create an awk script to subtract milliseconds between 2 records joined-up

  • 0

I’m trying to create an awk script to subtract milliseconds between 2 records joined-up for example:

By command line I might do this:

Input:

06:20:00.120
06:20:00.361
06:20:15.205
06:20:15.431
06:20:35.073
06:20:36.190
06:20:59.604
06:21:00.514
06:21:25.145
06:21:26.125

Command:

awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s ",$0) } }' input 

I’ll obtain this:

06:20:00.120 06:20:00.361
06:20:15.205 06:20:15.431
06:20:35.073 06:20:36.190
06:20:59.604 06:21:00.514
06:21:25.145 06:21:26.125

To substract milliseconds properly:

awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s ",$0) } }' input| awk -F':| ' '{print $3, $6}'

And to avoid negative numbers:

awk '{if ($2<$1) sub(/00/, "60",$2); print $0}'
awk '{$3=($2-$1); print $3}'

The goal is get this:

Call 1 0.241 ms
Call 2 0.226 ms
Call 3 1.117 ms
Call 4 0.91 ms
Call 5 0.98 ms

And finally and average.

I might perform this but command by command. I dunno how to place this into a script.

Please need help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:25:27+00:00Added an answer on May 28, 2026 at 1:25 pm

    One way using awk:

    Content of script.awk:

    ## For every input line.
    {
            ## Convert formatted dates to time in miliseconds.
            t1 = to_ms( $0 )
            getline
            t2 = to_ms( $0 )
    
            ## Calculate difference between both dates in miliseconds.
            tr = (t1 >= t2) ? t1 - t2 : t2 - t1
    
            ## Print to output with time converted to a readable format.
            printf "Call %d %s ms\n", ++cont, to_time( tr )
    }
    
    ## Convert a date in format hh:mm:ss:mmm to miliseconds.
    function to_ms(time,    time_ms, time_arr)
    {
            split( time, time_arr, /:|\./ )
            time_ms = ( time_arr[1] * 3600 + time_arr[2] * 60 + time_arr[3] ) * 1000 + time_arr[4]
            return time_ms
    }
    
    
    ## Convert a time in miliseconds to format hh:mm:ss:mmm. In case of 'hours' or 'minutes'
    ## with a value of 0, don't print them.
    function to_time(i_ms,         time)
    {
            ms = int( i_ms % 1000 )
            s = int( i_ms / 1000 )
            h = int( s / 3600 )
            s = s % 3600
            m = int( s / 60 )
            s = s % 60
    #       time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." ms
            time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." sprintf( "%03d", ms )
            return time
    }
    

    Run the script:

    awk -f script.awk infile
    

    Result:

    Call 1 0.241 ms
    Call 2 0.226 ms
    Call 3 1.117 ms
    Call 4 0.910 ms
    Call 5 0.980 ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.