我对 TCL 很陌生,需要一些帮助,根据来自 Cisco 路由器的日期戳将日志消息分组在一起。
更新:示例日志中的更改。发现当 DD 是个位数时有一个额外的空格。例如“1”
Samplelog : 显示日志
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: OFFLINE
THREADID: Feb 1 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 3 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Feb 3 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Feb 4 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Feb 4 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 15 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Mar 15 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 15 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 16 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: UP
THREADID: Mar 16 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
THREADID: Mar 16 HH:MM:SS.SSS : %TYPE-OF-VAIRBLE: DOWN
期望的输出
Feb 01 || 3 down
Feb 04 || 2 down
Mar 15 || 2 down
Mar 16 || 2 down
使用新逻辑更新了新代码,以尝试解决额外空间,尽管这没有按预期工作。
set y [read -nonewline [set f [open "tmp.txt" r]]];
array set counts [list];
foreach message [split $y "\n"] {
# This gets the status, ie: DOWN/UP/OFFLINE.
set status [lindex [split $message] end]; # will assign last field
set mon [lindex [split $message] 1]; # number represents the filed number
set day [lindex [split $message] 2];
if {$day ==""} {
set day [lindex [split $message] 3];
} else {
set day [lindex [split $message] 2]
}
# +number represents the counter
if {[info exists counts($mon,$day,$status)]} {
set counts($mon,$day,$status) [expr {$counts($mon,$day,$status)+1}]
} else {
set counts($mon,$day,$status) 1
}
}
# sort based for down type events
puts "\n\n MMM DD || Cnt Status"
puts " ====================="
foreach count [lsort -increasing -unique [array names counts]] {
foreach {mon day status} [split $count ","] { break; }
if {$status =="down"} {
puts " $mon $day || [set counts($count)] \t $status"
}
}
if {[info exists f]} { close $f }