如何使用AWK-script减去毫秒

如何使用AWK-script减去毫秒,awk,Awk,我正在尝试创建一个awk脚本,以在连接的两条记录之间减去毫秒,例如: 通过命令行,我可以执行以下操作: 输入: 06:20:00.120 06:20:00.361 06:20:15.205 06:20:15.431 06:20:35.073 06:20:36.190 06:20:59.604 06:21:00.514 06:21:25.145 06:21:26.125 命令: awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else {

我正在尝试创建一个awk脚本,以在连接的两条记录之间减去毫秒,例如:

通过命令行,我可以执行以下操作:

输入:

06:20:00.120
06:20:00.361
06:20:15.205
06:20:15.431
06:20:35.073
06:20:36.190
06:20:59.604
06:21:00.514
06:21:25.145
06:21:26.125
命令:

awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s ",$0) } }' input 
我会得到这个:

06:20:00.120 06:20:00.361
06:20:15.205 06:20:15.431
06:20:35.073 06:20:36.190
06:20:59.604 06:21:00.514
06:21:25.145 06:21:26.125
要正确减去毫秒数,请执行以下操作:

awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s ",$0) } }' input| awk -F':| ' '{print $3, $6}'
为避免出现负数:

awk '{if ($2<$1) sub(/00/, "60",$2); print $0}'
awk '{$3=($2-$1); print $3}'
最后是平均水平

我可能会执行这个命令,但不是一个命令接一个命令。我不知道如何将其放入脚本中


请需要帮助。

使用
awk

awk '
BEGIN { cmd = "date +%s.%N -d " }
NR%2  {
    cmd $0 | getline var1;
    next
}
{
    cmd $0 | getline var2;
    var3 = var2 - var1;
    print "Call " ++i, var3 " ms"
}
' file
Call 1 0.241 ms
Call 2 0.226 ms
Call 3 1.117 ms
Call 4 0.91 ms
Call 5 0.98 ms
## For every input line.
{
        ## Convert formatted dates to time in miliseconds.
        t1 = to_ms( $0 )
        getline
        t2 = to_ms( $0 )

        ## Calculate difference between both dates in miliseconds.
        tr = (t1 >= t2) ? t1 - t2 : t2 - t1

        ## Print to output with time converted to a readable format.
        printf "Call %d %s ms\n", ++cont, to_time( tr )
}

## Convert a date in format hh:mm:ss:mmm to miliseconds.
function to_ms(time,    time_ms, time_arr)
{
        split( time, time_arr, /:|\./ )
        time_ms = ( time_arr[1] * 3600 + time_arr[2] * 60 + time_arr[3] ) * 1000 + time_arr[4]
        return time_ms
}


## Convert a time in miliseconds to format hh:mm:ss:mmm. In case of 'hours' or 'minutes'
## with a value of 0, don't print them.
function to_time(i_ms,         time)
{
        ms = int( i_ms % 1000 )
        s = int( i_ms / 1000 )
        h = int( s / 3600 )
        s = s % 3600
        m = int( s / 60 )
        s = s % 60
#       time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." ms
        time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." sprintf( "%03d", ms )
        return time
}

单向使用
awk

awk '
BEGIN { cmd = "date +%s.%N -d " }
NR%2  {
    cmd $0 | getline var1;
    next
}
{
    cmd $0 | getline var2;
    var3 = var2 - var1;
    print "Call " ++i, var3 " ms"
}
' file
Call 1 0.241 ms
Call 2 0.226 ms
Call 3 1.117 ms
Call 4 0.91 ms
Call 5 0.98 ms
## For every input line.
{
        ## Convert formatted dates to time in miliseconds.
        t1 = to_ms( $0 )
        getline
        t2 = to_ms( $0 )

        ## Calculate difference between both dates in miliseconds.
        tr = (t1 >= t2) ? t1 - t2 : t2 - t1

        ## Print to output with time converted to a readable format.
        printf "Call %d %s ms\n", ++cont, to_time( tr )
}

## Convert a date in format hh:mm:ss:mmm to miliseconds.
function to_ms(time,    time_ms, time_arr)
{
        split( time, time_arr, /:|\./ )
        time_ms = ( time_arr[1] * 3600 + time_arr[2] * 60 + time_arr[3] ) * 1000 + time_arr[4]
        return time_ms
}


## Convert a time in miliseconds to format hh:mm:ss:mmm. In case of 'hours' or 'minutes'
## with a value of 0, don't print them.
function to_time(i_ms,         time)
{
        ms = int( i_ms % 1000 )
        s = int( i_ms / 1000 )
        h = int( s / 3600 )
        s = s % 3600
        m = int( s / 60 )
        s = s % 60
#       time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." ms
        time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." sprintf( "%03d", ms )
        return time
}
script.awk的内容

awk '
BEGIN { cmd = "date +%s.%N -d " }
NR%2  {
    cmd $0 | getline var1;
    next
}
{
    cmd $0 | getline var2;
    var3 = var2 - var1;
    print "Call " ++i, var3 " ms"
}
' file
Call 1 0.241 ms
Call 2 0.226 ms
Call 3 1.117 ms
Call 4 0.91 ms
Call 5 0.98 ms
## For every input line.
{
        ## Convert formatted dates to time in miliseconds.
        t1 = to_ms( $0 )
        getline
        t2 = to_ms( $0 )

        ## Calculate difference between both dates in miliseconds.
        tr = (t1 >= t2) ? t1 - t2 : t2 - t1

        ## Print to output with time converted to a readable format.
        printf "Call %d %s ms\n", ++cont, to_time( tr )
}

## Convert a date in format hh:mm:ss:mmm to miliseconds.
function to_ms(time,    time_ms, time_arr)
{
        split( time, time_arr, /:|\./ )
        time_ms = ( time_arr[1] * 3600 + time_arr[2] * 60 + time_arr[3] ) * 1000 + time_arr[4]
        return time_ms
}


## Convert a time in miliseconds to format hh:mm:ss:mmm. In case of 'hours' or 'minutes'
## with a value of 0, don't print them.
function to_time(i_ms,         time)
{
        ms = int( i_ms % 1000 )
        s = int( i_ms / 1000 )
        h = int( s / 3600 )
        s = s % 3600
        m = int( s / 60 )
        s = s % 60
#       time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." ms
        time = (h != 0 ? h ":" : "")  (m != 0 ? m ":" : "") s "." sprintf( "%03d", ms )
        return time
}
运行脚本:

awk -f script.awk infile
结果:

Call 1 0.241 ms
Call 2 0.226 ms
Call 3 1.117 ms
Call 4 0.910 ms
Call 5 0.980 ms

如果您不受awk限制:

to_epoch() { date -d "$1" "+%s.%N"; }
count=0
paste - - < input |
while read t1 t2; do
    ((count++))
    diff=$(printf "%s-%s\n" $(to_epoch "$t2") $(to_epoch "$t1") | bc -l)
    printf "Call %d %5.3f ms\n" $count $diff
done
to_epoch(){date-d“$1”+%s.%N”}
计数=0
粘贴---<输入|
同时读取t1和t2;做
((count++)
diff=$(printf“%s-%s\n”$(到_epoch“$t2”)$(到_epoch“$t1”)| bc-l)
printf“呼叫%d%5.3f ms\n”$count$diff
完成

您知道如何制作
bash
脚本(或者通常只是一个shell脚本)吗?这是第一步,您可以通过管道将
awk
命令连接在一起。是的,我已经做过了,并且可以正常工作。但我只是试着把它放在awk脚本中,我试过了,但我不明白逻辑部分是什么。一个解决方案是将hr:mm:ss“标准化”为秒值,并将ms部分保留为小数。减去这两个值,然后将该值(小数点左侧)反规格化回小时、分钟、秒,然后打印为hh:mm:ss.ms。祝你好运。@Sheller你很好,先生!!:)+1个很好的一行代码,外加
date
外部命令的宝贵帮助。我不知道如何对大家说谢谢!你的帖子帮了我太多忙了@马丁:当你的声誉达到16分时,你可以把所有这些好的答案都投给你。你的问题表达得很好,你展示了你的输入,预期的输出,以及你迄今为止尝试过的代码。请注意@DanFergo如何重新格式化您的问题,以便于阅读,以供将来参考。重新格式化工具位于输入文本框的左上角。“地球仪”{}。。。什么的。你的语言很好。祝你好运。我不知道如何为大家说谢谢!你的帖子帮了我太多忙了!我在这个脚本中看到一些难以理解的内容,例如:00:57:47.903 00:57:48.919这次的结果应该是1.063毫秒,但是脚本打印的是1.63毫秒,非常不同。我不知道它为什么这样做,因为只有小时和分钟被声明为不显示0。我是sry,那些值00:57:47.903 00:57:48.919应该显示这个值:“呼叫1 1.016毫秒”,但它显示这个值:“呼叫1 1.16毫秒”。@Martin_911:我已编辑以解决设置毫秒字段格式的问题。左起错线。我希望它现在能工作。@Martin_911:Hours and minutes不显示何时为零,因为我在您的示例中没有看到它们,但是如果需要的话,改变它的行为似乎并不困难。我不知道如何为大家说声谢谢!你的帖子帮了我太多忙了!