Nwn
Nwn

Reputation: 571

Need to subtract immediate line time deference in a log file in bash?

I have a log file which is genarated from a backup restoring process. I want know whether is there any slowness compared to the previous times. so i need to the time deference for each an individual restore module time . here is my log file.

 2019-10-26 08:06:56 6503  6501 Begin restore logical log 2290281 (Storage Manager copy ID: 1 1571839465).
 2019-10-26 08:14:05 6503  6501 Completed restore logical log 2290281.
 2019-10-26 08:14:09 6503  6501 Begin restore logical log 2290282 (Storage Manager copy ID: 1 1571839691).
 2019-10-26 08:21:09 6503  6501 Completed restore logical log 2290282.
 2019-10-26 08:21:13 6503  6501 Begin restore logical log 2290283 (Storage Manager copy ID: 1 1571839892).

so my expectation is showing simply below result

deference between (2019-10-26 08:14:05) and  (2019-10-26 08:06:56) 
deference between (2019-10-26 08:21:09) and  (2019-10-26 08:14:09) 

so my expected output is

7 minutes and 11 seconds
7 minutes and 00 seconds 

Upvotes: 0

Views: 38

Answers (1)

dash-o
dash-o

Reputation: 14448

You can use bash/date for the simple date math.

PREV=
DELTA=0
while read dt  tm data ; do
  CURR=$(date '+%s' -d "$dt $tm");
  [ "$PREV" ] && DELTA=$((CURR-PREV));
  PREV=$CURR ;
  echo "$DELTA sec $dt $tm $data" ;
done < logfile

Additional formatting can be added to the echo

echo "$((DELTA/60)) min and $((DELTA%60)) sec $dt $tm $data" ;

Output:
0 min and 0 sec 2019-10-26 08:06:56 6503  6501 Begin restore logical log 2290281 (Storage Manager copy ID: 1 1571839465).
7 min and 9 sec 2019-10-26 08:14:05 6503  6501 Completed restore logical log 2290281.
0 min and 4 sec 2019-10-26 08:14:09 6503  6501 Begin restore logical log 2290282 (Storage Manager copy ID: 1 1571839691).
7 min and 0 sec 2019-10-26 08:21:09 6503  6501 Completed restore logical log 2290282.

Upvotes: 1

Related Questions