开发者

Awk: How to cut similar part of 2 fields and then get the difference of remaining part?

开发者 https://www.devze.com 2023-04-11 19:46 出处:网络
Let say I have 2 fields displaying epoch time 开发者_Python百科in microseconds: 1318044415123456,1318044415990056

Let say I have 2 fields displaying epoch time 开发者_Python百科in microseconds:

1318044415123456,1318044415990056

What I wanted to do is:

  1. Cut the common part from both fields: "1318044415"
  2. Get the difference of the remaining parts: 990056 - 123456 = 866600

Why am I doing this? Because awk uses floating point IEEE 754 but not 64 bit integers and I need to get difference of epoch time of 2 events in microseconds.

Thanks for any help!

EDIT:

Finally I found the largest number Awk could handle on Snow Leopard 10.6.8: 9007199254740992.

Try this: echo '9007199254740992' | awk -F ',' '{print $1 + 0}'

The version of Awk was 20070501 (produced by awk --version)


Here is an awk script that meets your requirements:

BEGIN {
    FS = ","
}
{
    s1 = $1
    s2 = $2
    while (length(s1) > 1 && substr(s1, 1, 1) == substr(s2, 1, 1))
    {
        s1 = substr(s1, 2)
        s2 = substr(s2, 2)
    }
    n1 = s1 + 0
    n2 = s2 + 0
    print n2 - n1
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号