【问题标题】:How to subtract the difference of two timestamps in a 2 log files如何减去 2 个日志文件中两个时间戳的差异
【发布时间】:2013-06-19 04:03:16
【问题描述】:

我试图找出通过我的系统发送文件需要多长时间:我在两个日志文件中记录了时间戳,如下所示:

第一

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

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

以下是我用来减去时间戳的命令。

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 s。似乎它正在做与 EPOCH 时间的差异。有人可以帮忙吗?

【问题讨论】:

标签: linux bash scripting awk grep


【解决方案1】:

你在问题​​中提到有两个日志文件,即使你只发布了一个。因此,我将以您的输入数据为例,向您展示如何通过自己的方式找到解决方案。

根据新的示例数据更新了解决方案。

使用的样本数据:

$ 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

我添加了 cmets 使其更容易理解。

script.awk 的内容:

# 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个日志文件,没有...的差距是它们的区别。抱歉,不清楚。
  • @user2019182 不客气。我已根据您更新的问题和示例数据对答案进行了必要的更改。祝你好运
  • 嘿,效果很好,谢谢。在脚本中我也可以打印文件大小吗?尺寸标志是多少?打印文件开始,差异,
  • 不客气。要获取文件大小,您可以使用 start[$1"\t"$3] 和 end[$1"\t"$3] 作为数组。休息一切都应该是一样的。我刚刚在您的示例数据中注意到,尽管ITAM.xml 的文件名相同,但大小不同。在这种情况下,它不会打印出来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
相关资源
最近更新 更多