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?
The command date +%S will print the current amount of seconds.
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.
$() 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.
$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.
$ TZ=utc date -d '1970-01-01 1:43:38' +%s
6218
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With