What's an easy way to convert 00:20:40.28
(HH:MM:SS) to seconds with a Bash script?
Split seconds can be cut out, it’s not essential.
What's an easy way to convert 00:20:40.28
(HH:MM:SS) to seconds with a Bash script?
Split seconds can be cut out, it’s not essential.
You can convert minutes to hour, seconds, or minutes with
bc
command.By example:
How many minutes for 1000 sec ?
then -> 2 min
How much hour for 375 min ?
then -> 06h15
How days for 56 hours?
then 02 days and 08 hours
bc with obase is extra!
I haven't tested this but, I think this is how you'd split the string. Followed by multiplying by the appropriate amounts for hours and minutes.
I have this old shell function (/bin/sh compatible in the sense of POSIX shell, not bash) which does this conversion in integer math (no fractions in the seconds):
Outputs:
It works with SS, MM:SS and HH:MM:SS only :)
Try this:
($<string>) splits <string> based on the splitter (
IFS
).${<array>[<index>]} returns the element of the <array> at the <index>.
$((<arithmetic expression>)) performs the arithmetic expression.
Hope this helps.
Try
awk
. As a bonus, you can keep the split seconds.