計算兩個倍(包括millseconds)之間的差I具有輸出每行兩個時間戳像這樣一個簡單的bash腳本:在bash
10:25:48,192 10:25:46,967
10:25:48,522 10:25:48,200
10:25:49,418 10:25:48,531
我想縮小腳本計算差值(以毫秒爲單位)在每條線上的兩個時間戳之間。如何才能做到這一點?
計算兩個倍(包括millseconds)之間的差I具有輸出每行兩個時間戳像這樣一個簡單的bash腳本:在bash
10:25:48,192 10:25:46,967
10:25:48,522 10:25:48,200
10:25:49,418 10:25:48,531
我想縮小腳本計算差值(以毫秒爲單位)在每條線上的兩個時間戳之間。如何才能做到這一點?
#!/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
輸出:
1225 322 887
使用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
這可能有助於與GNU日期:自毫秒爲單位:'日期+%S%3N' – Cyrus
啊,謝謝。這可能會幫助我用awk方法嘗試工作。 – GaZ