gpt4 book ai didi

将毫秒转换为天的 Bash 脚本 :Hours:Minutes:Seconds:Milliseconds

转载 作者:行者123 更新时间:2023-12-03 23:53:48 24 4
gpt4 key购买 nike

我编写了以下 bash 脚本将毫秒转换为 Days:Hours:Minutes:Seconds:Milliseconds 以使日志文件更具可读性:

    #!/bin/bash
### Constants ###
CON_DAYS=.0000000115741
CON_HOURS=.000000277778
CON_MINUTES=.000066667
CON_SECONDS=.001

### Variables ###
INPUT="$1"

cat $INPUT | awk -v CON_HOURS=$CON_HOURS -v CON_MINUTES=$CON_MINUTES -v CON_SECONDS=$CON_SECONDS -v CON_DAYS=$CON_DAYS '
{ $1=substr($0,0,10) }
{ MILLISECONDS = $1 }
{ DAYS = int(MILLISECONDS * CON_DAYS) }
{ MILLISECONDS = MILLISECONDS - int( DAYS / CON_DAYS ) }
{ HOURS = int(MILLISECONDS * CON_HOURS) }
{ MILLISECONDS = MILLISECONDS - int(HOURS / CON_HOURS) }
{ MINUTES = int(MILLISECONDS * CON_MINUTES) }
{ MILLISECONDS = MILLISECONDS - int(MINUTES / CON_MINUTES) }
{ SECONDS = int(MILLISECONDS * CON_SECONDS) }
{ MILLISECONDS = MILLISECONDS - int( SECONDS / CON_SECONDS ) }
{ $1 = DAYS":"HOURS":"MINUTES":"SECONDS":"MILLISECONDS"ms" }
{print}'
exit

输入文件部分:

1882224617mS ATMChannel: [1] CMLinkLayer Rx: 'DialDigits' (ls)
1882224617mS ATMIO: [1] TONE DIAL (11 digits)
1882224617mS ATMChannel: [1] StateChange Connected->ToneDialing

有几行输出表明它工作不正常:

22:19:224:14:186ms ATMChannel: [1] CMLinkLayer Rx: 'DialDigits' (ls)
22:19:224:14:186ms ATMIO: [1] TONE DIAL (11 digits)
22:19:224:14:186ms ATMChannel: [1] StateChange Connected->ToneDialing

经过几个小时的故障排除后,我无法找到我的错误。任何帮助将不胜感激。

最佳答案

分为毫秒和秒,使用GNU日期,你应该更容易得到结果

#!/bin/bash

INPUT="$1"

while read t rest
do
ms=$(echo $t|sed -r "s/.*(.....)$/\1/") # get Milliseconds 617mS
se=$(echo $t|sed -r "s/(.....)$//") # get seconds 1882224
days=$(echo $se / 3600 / 24 |bc)

d="$days:$(date -d "1970-01-01 $se seconds" +%H:%M:%S):$ms"
echo "$d $rest"
done < $INPUT

关于将毫秒转换为天的 Bash 脚本 :Hours:Minutes:Seconds:Milliseconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21505139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com