1

我有一个日志,它同时被 1 个或多个进程转储。有时,由于 CPU 过载和上下文切换,记录到该文件会被延迟。我想找到每行之间的时间差并在每行之前打印出来?

日志示例:

07/18 16:20:29886564 Pid= 2998,Tid= 3036,  XXXXX.c:  335:XXXXX:### xxxxxxxxxxxxxxxxxx ### 
07/18 16:20:29886642 Pid= 2998,Tid= 3036,  XXXXX.c:  484:XXXXX:### yyyyyyyyyyyyyyy() 
07/18 16:20:29886880 Pid= 2998,Tid= 3036,  XXXXX.c:  488:XXXXX:>>>yyyyyyyyyyyyy() 
07/18 16:20:29887002 Pid= 2998,Tid= 3036,  XXXXX.c:  494:XXXXX:>>>OK: zzzzzzzzzzzzzzz()

我想这可以通过'awk'来实现。但是,我不擅长 linux 命令。有人可以帮忙吗?

4

1 回答 1

2

您可以尝试以下awk命令。我已对其进行了评论,以便您了解它的工作原理:

awk '{
  # split the time field
  split($2,arr,":"); 

  # convert hours and mins into seconds and compute total
  curr=arr[3]+arr[2]*60+arr[1]*60*60;

  # set previous value for the first line
  if(prev == 0) prev=curr;

  # print out the difference between current and previous totals
  print curr-prev,$0;

  # set previous to current
  prev=curr;
}' file
于 2013-07-22T08:54:40.100 回答