2017-02-15 83 views
-1

什麼是算最有效的方法,措施(計算)兩個時間戳從日誌文件之間之間的時間跨度來衡量的時間跨度最好的方法是什麼? 我知道Excel/Numbers/Refine中的愚蠢解決方案,但我正在尋找一個聰明的awk-oneliner。請告訴我兩個時間戳

示例: 的自動門系統的電梯n個樓層操作。

電梯打開門:

Start Move to Work_position

的電梯門打開時:

End Move to Work_position

電梯關閉門

Start Move to Home_Position End Move to Home_Position

的(製備)日誌文件:
Elevator_021 logger: 2017-02-13 13:11:33.735 [MONITORING] CYL_A1; Floor_12 Door ;Start Move to Work_position 
Elevator_021 logger: 2017-02-13 13:11:34.800 [MONITORING] CYL_A1; Floor_12 Door ;End Move to Work_position 

Elevator_021 logger: 2017-02-13 13:26:12.893 [MONITORING] CYL_A1; Floor_12 Door ;Start Move to Home_Position 
Elevator_021 logger: 2017-02-13 13:26:13.898 [MONITORING] CYL_A1; Floor_12 Door ;End Move to Home_Position 

Elevator_021 logger: 2017-02-13 14:51:03.211 [MONITORING] CYL_A1; Floor_12 Door ;Start Move to Work_position 
Elevator_021 logger: 2017-02-13 14:51:04.276 [MONITORING] CYL_A1; Floor_12 Door ;End Move to Work_position 

我盼到像這樣的輸出:

Elevator_021 logger: 2017-02-13 13:11:33.735 [MONITORING] CYL_A1; Floor_12 Door ;Start Move to Work_position 
Elevator_021 logger: 2017-02-13 13:11:34.800 [MONITORING] CYL_A1; Floor_12 Door ;End Move to Work_position 
to Work 1065 ms 

Elevator_021 logger: 2017-02-13 13:26:12.893 [MONITORING] CYL_A1; Floor_12 Door ;Start Move to Home_Position 
Elevator_021 logger: 2017-02-13 13:26:13.898 [MONITORING] CYL_A1; Floor_12 Door ;End Move to Home_Position 
to Home 1005 ms 

Elevator_021 logger: 2017-02-13 14:51:03.211 [MONITORING] CYL_A1; Floor_12 Door ;Start Move to Work_position 
Elevator_021 logger: 2017-02-13 14:51:04.276 [MONITORING] CYL_A1; Floor_12 Door ;End Move to Work_position 
to Work 3976 ms 

回答

1

你可以使用gawk做這樣的事情:

awk 'function dtime(s) { 
     x=match($0, /([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9:]+)(\.[0-9]+).*([0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9:]+)(\.[0-9]+).*(to \w+)_/, arr) 
     if (x) { 
       s1=arr[1] 
       s2=arr[3] 
       gsub(/[-:]/, " ", s1) 
       gsub(/[-:]/, " ", s2) 
       t1=mktime(s1)+arr[2] 
       t2=mktime(s2)+arr[4] 
       return arr[5] " " (t2-t1)*1000 " ms" 
     } 
     else 
       return "" 
     } 
BEGIN {RS=ORS="\n\n"} 
     {print $0 " -> " dtime($0) }' file 

打印:

Elevator_021記錄: 2017-02-13 13:11:33.735 [監測] CYL_A1; Floor_12 Door;開始移動到Work_position Elevator_021 logger:2017-02-13 13:11:34.800 [MONITORING] CYL_A1;工作位置 - >工作1065 ms

Elevator_021 logger:2017-02-13 13:26:12.893 [MONITORING] CYL_A1; Floor_12 Door;開始移動到Home_Position Elevator_021 logger:2017-02-13 13:26:13.898 [MONITORING] CYL_A1; Floor_12 Door; End移動到Home_Position - >返回首頁1005 ms

Elevator_021 logger:2017-02-13 14:51:03.211 [MONITORING] CYL_A1; Floor_12 Door;開始移動到Work_position Elevator_021 logger:2017-02-13 14:51:04.276 [MONITORING] CYL_A1; Floor_12 Door; End移動到Work_position - >工作1065 ms

+0

thx! 但是,不! \t AWK:在功能DTIME上下文源極線2語法錯誤爲x =匹配($ 0,/([0-9] {4} - [0-9] {2} - [0-9] {2} \ [0-9:] +)(\。\t [0-9] +)。*([0-9] {4} - [0-9] {2} - [0-9] { 2} \ s [0-9:] +)(\。[0-9] +)。*(to >>> \ w +)_ /,<<< awk:source line 2 \t在功能DTIME AWK:在源線2的Funktion之聲 –

+0

非法聲明這將是從1個襯墊在POSIX awk的,因爲它沒有時間的功能遠你可以調查使用'date'的時間戳轉換或使用Perl或。安裝GAWK – dawg

+0

它的工作原理。 - 切換到另一個OS-X機 但僅與相關線的「預分組」 - 需要相關對之間的一個空行 –