I want to do parsing of Elapsed time in seconds .Time formats given below:
1) 3 day 18h
2) 3 day
3) 3h 15min
4) 3h
5) 15min 10sec
6) 15min
7) 10sec
i'm getting values from systemctl status cassandra | awk '/(Active: active)/{print $9, $10,$11}' Now storing it's value in variable A,like
A=$(systemctl status cassandra | awk '/(Active: active)/{print $9, $10,$11}'
now A has input as 3 day 18h or 3 day etc. More examples-
A=3 day 18h or 3 day or 3h 15min or 3h or 15min 10sec or 15min or 10sec
now take different values of A, and parse in seconds.
What you want to achieve could be done directly in
awkusing the following line :This will give you the running time directly based on the start-time and not on the approximated running time printed by
systemctl.If this approach is not working then I suggest to use the
datecommand to do all the parsing. If you can change thehbyhourin your examples, then you can do the following :If you cannot, then I suggest the following. If duration is stored in the variable
$duration, then you doHaving spaces between the numbers and the strings
day,h,minorsecdoes not matter.The idea of this is that you ask
dateto compute everything for you as%sreturns the unix time since 1970-01-01 in seconds.