价格变动 - 过去 1 小时变化 - 在 Bash 中查找过去 1 小时价值的最佳方式



带有时间戳和值的示例数据库文件 (dbfile(:

1516307965 598917802507
1516312165 584337425999
1516324465 584147968883
1516341864 568759422044
1516368865 596288892045`

我的代码:

last=``date +%s``
   lasth=$(($last-3600))
   lasthdown=$(($lasth-120))
   lasthup=$(($lasth+120))
   tbh=`sort -u /home/f/dbfile | awk -v lasthdown="$lasthdown" -v lasthup="$lasthup" '$1 > lasthdown && $1 < lasthup' | head -1 | awk '{print $2}'``
   tpr1h=$(bc -l <<< "scale=1;100*($db-$tbh)/$db")`

也许您知道更多匹配过去 1 小时值的优先方式......

由于时间戳采用纪元格式,因此您可能希望简单地省略st变量中的最后 2 位数字并搜索文件,这样您只有 99 秒(1 分 39 秒(的精度。您可以使用以下 3 行来实现这一点:

#get the epoch time stamp from 1 hour ago
st=`date -d '1 hour ago' +%s`
#obtain only the first 8 digits (the 99 second precision)
st1=`echo $st | awk '{print substr($0,1,8);exit}'`
# search the file for the above hour and sort the output to obtain the closest match, then print only the value in the 2nd column for that match
tbh=`grep "${st1}[0-9]{2}" ./db.txt| sort -k1 -nu | awk 'END{print $2}'`
echo $tbh

注意:您可以通过更改以下行以省略时间戳的最后 3 位数字来进一步降低精度(或根据需要增加精度(,从而为您提供 999 秒的精度。

st1=`echo $st | awk '{print substr($0,1,7);exit}'

最新更新