Issue
```
I have a string in the below format aYbMTcDdHeMfs where a,b,c,d,e and f are numerics of any length. Y-> Years MT-> Months D-> days H-> hours M-> minutes S-> Seconds
```
The string can have anything like
1H2M3S
1Y2MT3D4H5H6S
1Y20S
.. so on.
Here the input has the same order of years,months,days,hours,minutes and seconds.
If we want to fetch the value of years, months, days, hours, minutes, and seconds from the string then what would be the shell script to convert it into seconds?
1H20S -> 3620
10M200S -> 800
10M100S -> 700
Can you help me with the script to find those?
Solution
Your specification is incomplete but here's a couple of ideas to get you started:
#!/bin/bash
aYbMTcDdHeMfS(){
x=${1//Y/*31536000+}
x=${x//MT/*2628000+}
x=${x//D/*86400+}
x=${x//H/*3600+}
x=${x//M/*60+}
x=${x//S/+}
x=${x%+}
echo "$((x))"
}
aYbMTcDdHeMfS 1H # 3600
aYbMTcDdHeMfS 15S3M # 195
aYbMTcDdHeMfS 1H2M3S # 3723
aYbMTcDdHeMfS 1Y2MT3D4H5H6S # 37083606
aYbMTcDdHeMfS 1Y20S # 31536020
aYbMTcDdHeMfS 1H20S # 3620
aYbMTcDdHeMfS 10M200S # 800
aYbMTcDdHeMfS 10M100S # 700
<<'EOD' sed '
s/[[:space:]]*//g
s/Y/*31536000+/g
s/MT/*2628000+/g
s/D/*86400+/g
s/H/*3600+/g
s/M/*60+/g
s/S/+/g
s/+*$//
' | bc -l
1H
15S3M
1H2M3S
1Y2MT3D4H5H6S
1Y20S
1H20S
10M200S
10M100S
EOD
Answered By - jhnc