I am currently working on a script which processes a csv file, and corrects certain aspects of them along the way. One of the things that it does is correct time format if needed. Two type of conversion takes place:
xx:xx:xx to PTxxHxxMxxS
10:03:45 to PT10H03M45S
I’ve have been able to do this using the following (see below) although I am trying to find out how to do it either using sed or awk in order to speed up the process. In addition to the actual conversion process, I would also like to keep count of the changes that are made (so say 4 times values are converted, a counter would be incremented to 4), which I have been able to do easily with the if statement below (though it is not shown), although I would not know much about doing that using sed/awk.
istimef=$( echo "$Sfcpp6" | grep ".*:.*:.*" )
if [ "$istimef" != "" ]; then
hs=$( echo "$Sfcpp6" | cut -d ':' -f 1 )
mn=$( echo "$Sfcpp6" | cut -d ':' -f 2 )
sc=$( echo "$Sfcpp6" | cut -d ':' -f 3 )
Sfcpp6=$( echo "PT"$hs"H"$mn"M"$sc"S" )
echo "$Sfcp6"
fi
which essentially checks if the time value is even there, and then performs the conversion.
sedsolution: uses\(...\)to capture the numbers, character classes[0-9]to match any digit.