Saran-san
Saran-san

Reputation: 360

Command to find the time difference

I have a log, which is dumped by 1 or more process simultaneously. Sometimes, because of CPU over load and context switching, logging to this file will be delayed. I want to find the time difference between each line and print it before each line?

Log example:

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()

I suppose that this could be possible with 'awk'. But, I am pretty bad at linux commands. Could someone please help with this?

Upvotes: 1

Views: 243

Answers (1)

dogbane
dogbane

Reputation: 274612

You can try the following awk command. I have commented it so you can understand how it works:

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

Upvotes: 2

Related Questions