Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HH:MM:SS.mm to seconds in bash

Tags:

bash

I am running some gnu time scripts which generates output of the form mm:ss.mm (minutes, seconds and miliseconds, for example 1:20.66) or hh:MM:ss (hours, minutes and seconds, for example 1:43:38). I want to convert this to seconds (in order to compare them and plot them in a graphic).

Which is the easiest way to do this using bash?

like image 557
Luixv Avatar asked Feb 07 '12 14:02

Luixv


People also ask

How do I convert time to seconds in bash?

The command date +%S will print the current amount of seconds.

How do you get seconds from HH MM SS?

To convert hh:mm:ss to seconds:Convert the hours to seconds, by multiplying by 60 twice. Convert the minutes to seconds by multiplying by 60 . Sum the results for the hours and minutes with the seconds to get the final value.

What is $() in bash?

$() means: "first evaluate this, and then evaluate the rest of the line". Ex : echo $(pwd)/myFile.txt. will be interpreted as echo /my/path/myFile.txt. On the other hand ${} expands a variable.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.


1 Answers

$ TZ=utc date -d '1970-01-01 1:43:38' +%s
6218
like image 185
kev Avatar answered Sep 20 '22 13:09

kev