Reputation: 2406
I have a simple bash script which outputs two timestamps per line like so:
10:25:48,192 10:25:46,967
10:25:48,522 10:25:48,200
10:25:49,418 10:25:48,531
I'd like to refine the script to calculate the difference (in milliseconds) between the two timestamps on each line. How can this be done?
Upvotes: 1
Views: 781
Reputation: 88553
#!/bin/bash
declare -i d1 d2 diff # set integer flag
while IFS=":, " read -r h1 m1 s1 x1 h2 m2 s2 x2; do
# force decimal numbers without leading zeros and calculate milliseconds
d1=$((10#$h1))*60*60*1000+$((10#$m1))*60*1000+$((10#$s1))*1000+$((10#$x1))
d2=$((10#$h2))*60*60*1000+$((10#$m2))*60*1000+$((10#$s2))*1000+$((10#$x2))
diff=$d1-$d2
echo $diff
done < file
Output:
1225 322 887
With awk:
awk -F '[:, ]' '{print ($1*60*60*1000+$2*60*1000+$3*1000+$4)-($5*60*60*1000+$6*60*1000+$7*1000+$8)}' file
Upvotes: 3