Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am new to shell scripting.

This is the sample date and time stored in UNIX system.
here date part is common I will use shell script and cut this date part.

20221010042234.671 - 20221010042234.491
20221010132747.826 - 20221010132747.712
20221010060016.904 - 20221010060016.903

Input 
132747.826 - 132747.712 
060016.904 - 060016.903
042234.671 - 042234.491

Expected output
0.18
0.11
0.001

What I have tried:

<pre>Our UNIX system doesn't support mktime,use,my
date -d, most of the functions are missing.

I'm looking for assistance in writing a shell script to calculate the time difference.

awk -F, -v OFS=',' '{ print $1-$2 }' Simple subtraction is not working for the 00th and 59th minutes.
Posted
Updated 13-Oct-22 6:12am

1 solution

Depending on what tools you have available, you have a few options. Lets assume that you can put your two operands in arg1 and arg2 respectively then
Perl: perl -e "print $arg1 - $arg2"
bc: echo "scale=3; echo $arg1 - $arg2" | bc
awk: echo $arg1 $arg2 | awk '{ printf "%.3f", $1 - $2}'

The bc option will add a trailing newline, which may or may not be an issue. If you wish to add a newline then for perl insert \"\n\" at the end of the expression string and for awk modify the printf argument to "%.3f\n" Note that the bc and awk versions will always print 3 significant digits. If that's not what you need, then you'll have to trim the trailing zeros.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900