I have a list of time spans in seconds, and want to compute the time span
as hh:mm:nn
I am coding in bash and have coded the following. However, the results are
wrong as "%.0f" rounds the values.
Example:
ftm: 25793.5
tmspan(hrs,min,sec): 7.16 429.89 25793.50
hh: 7
mm: 10
ss: -0
tmspan 7:10:1193.50
tmspan_sec=`echo "($npt - 1) * $dlt" | bc -l`
tmspan_min=`awk -v tm=$tmspan_sec 'BEGIN {printf "%.2f", tm/60.0}'`
tmspan_hrs=`awk -v tm=$tmspan_sec 'BEGIN {printf "%.2f", tm/(60.0*60.0)}'`
echo "tmspan(hrs,min,sec): $tmspan_hrs $tmspan_min $tmspan_sec"
hh=`awk -v tm=$tmspan_sec 'BEGIN {printf "%.0f", tm / (60.0 * 60.0)}'`
echo "hh: $hh"
mm=`awk -v tm=$tmspan_sec -v thh=$hh \
'BEGIN {printf "%.0f", (tm-(thh*60.0*60.0))/60.0}'`
echo "mm: $mm"
ss=`awk -v tm=$tmspan_sec -v thh=$hh -v tmm=$mm \
'BEGIN {printf "%.0f", (tm-(thh*60.0*60.0)-(tmm*60.0))/60.0}'`
echo "ss: $ss"
RudiC
February 23, 2018, 7:46am
2
Why not
printf "%02d:%02d:%02d\n" $((25793 / 3600)) $((25793 % 3600 / 60 )) $((25793 % 3600 % 60))
07:09:53
Please be aware that bash
can do integer calculations only! If you need fractions of seconds, try
printf "%02d:%02d:%02d%s\n" $((${ftm%.*} / 3600)) $((${ftm%.*} % 3600 / 60 )) $((${ftm%.*} % 3600 % 60)) ${ftm#${ftm%.*}}
07:09:53.5
2 Likes
I have now stored the initial and final time as 24-hour hours, minutes and seconds in
variables `itm` and `ftm` respictively.
My files look as follows and want to introduce `itm` and `ftm` in the file name. I want to
introduce `itm` and `ftm` instead of the number before `.sac` to get something like
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.itm.ftm.sac
Is there any good way to do this?
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132909.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132909.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132934.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132934.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.13290945.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.1329095.sac
---------- Post updated at 10:15 AM ---------- Previous update was at 10:06 AM ----------
The following does not work well
echo "$stfl" | awk -F '.' '{for(i=1;i<=(NF-2);i++) print $i}'