如何减去在2日志文件两个时间戳的差 [英] How to subtract the difference of two timestamps in a 2 log files

查看:282
本文介绍了如何减去在2日志文件两个时间戳的差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到它需要多长时间通过我的系统发送一个文件:我有记录在两个日志文件的时间戳如下:

I am trying to find how long it takes to send a file through my system: I have the time stamps logged in two log files as follows:

第一个

4824597 1371853829 /home/customer1/ITAM.xml
.
.
.
.
4824597 1371854003 /home/customer46/ITAM.xml

第二

4824597 1371854003 /home/customer1/ITAM.xml
.
.
.
.
4824597 1371854003 /home/customer46/ITAM.xml

下面是我使用减去时间戳的命令。

Below are the commands I am using to subtract the timestamps.

awk '{
  sub(/:/," ",$2);
  t1=mktime(strftime("%Y %m %d")" "$2" 00");
  getline < "/root/ITAM.txt";
  sub(/:/," ",$2);
  t2=mktime(strftime("%Y %m %d")" "$2" 00");
  print $3": "t2-t1" s"
}' /root/fileSizelog.txt

我现在遇到的问题是,我得到奇怪的输出,如-7185759秒。现在看来似乎是做从EPOCH时间的差异。任何人都可以请帮助?

The problem I am having now is that I am getting strange outputs like -7185759 s. It seems like it is doing the diff from EPOCH time. Can anyone please help?

推荐答案

<击>你在问题​​中提到,有即使你只有一家两个日志文件。所以,我会接受你的输入数据作为例子来告诉你如何用自己的方式解决。

更新该解决方案基于新的样本数据。

Updated the solution based on new sample data.

$ cat first
824597 1371853829 /home/customer1/ITAM.xml
4824597 1371854003 /home/customer46/ITAM.xml

$ cat second
4824597 1371854003 /home/customer1/ITAM.xml
4824597 1371854003 /home/customer46/ITAM.xml

我添加注释,使其更容易一点理解。

I have added comments to make it a little easier to understand.

# This syntax in combination with next (seen below) allows us to work on the first file 
# entirely 

NF==FNR {

# we are indexing the filename and assign it start time value

    start[$3]=$2

# next allows us to skip the rest action statements

    next
}

# once the first log file is looped over we store the second log file in end array

{

    end[$3]=$2
} 

# End block is where we are doing most of our computation since we have scanned 
# through the two files and now are ready to calculate the difference

END {

# we iterate over the start array and pick an index value (that is a file)

    for (filestart in start) {

# we do the same for our second array

        for (fileend in end) {

# if the filename are same then we are ready to do the difference

        if (filestart == fileend) {

# we subtract start time from end time

            diff = end[fileend] - start[filestart];

# we use sprintf function to avoid printing the difference so that we can store it in a variable

            diff = sprintf("%dh:%dm:%ds",diff/(60*60),diff%(60*60)/60,diff%60)

# we print the filename and the time lag

            print filestart,diff

# we delete the filename indices to reduce the size of array for performance reasons

            delete start[filestart]
            delete end[fileend]
            }
        }
    }
} 

无论是运行脚本的awk -f script.awk log.file 或运行它:

$ awk '        
NR==FNR {
    start[$3]=$2
    next
} 
{
    end[$3]=$2
} 
END {
    for(filestart in start) {
        for(fileend in end) {
            if (filestart == fileend) {
                diff = end[fileend] - start[filestart];
                diff = sprintf("%dh:%dm:%ds",diff/(60*60),diff%(60*60)/60,diff%60)
                print filestart,diff
                delete start[filestart]
                delete end[fileend]
             }
        }
    }
}' first second
/home/customer46/ITAM.xml 0h:0m:0s
/home/customer1/ITAM.xml 0h:2m:54s

这篇关于如何减去在2日志文件两个时间戳的差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆