none
Помогите распарсить log-файл. RRS feed

  • Вопрос

  • Всем привет!
    Имеется лог-файл вида:

    Output file opened at  2-SEP-2014 11:00:12.
    %PMM-I-OUTOPEN, Opened output file SMSC$ROOT:[LOG]SMSC_COUNTERS_GHLR.14090211
    PMS> SHOW CLASS GHLR /COUNT
    Response from GHLR_11
                                     Received messages: 10
                        Received messages and rejected: 0
                                  Transmitted messages: 11
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 38           
    Response from GHLR_21
                                     Received messages: 10
                        Received messages and rejected: 0
                                  Transmitted messages: 11
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 39           
    Response from GHLR_31
                                     Received messages: 8
                        Received messages and rejected: 0
                                  Transmitted messages: 9
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 37             
    PMS> RESET CLASS GHLR /COUNT
    Response from GHLR_11
    Response from GHLR_21
    Response from GHLR_31
    Closing output file at  2-SEP-2014 11:00:14.
    %PMM-I-OUTCLOSE, Output file closed, writes to file switched off
    Output file opened at  2-SEP-2014 11:05:10.
    %PMM-I-OUTOPEN, Opened output file SMSC$ROOT:[LOG]SMSC_COUNTERS_GHLR.14090211
    PMS> SHOW CLASS GHLR /COUNT
    Response from GHLR_11
                                     Received messages: 10
                        Received messages and rejected: 0
                                  Transmitted messages: 11
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 48             
    Response from GHLR_21
                                     Received messages: 12
                        Received messages and rejected: 0
                                  Transmitted messages: 13
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 49             
    Response from GHLR_31
                                     Received messages: 15
                        Received messages and rejected: 0
                                  Transmitted messages: 16
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 52             
    PMS> RESET CLASS GHLR /COUNT
    Response from GHLR_11
    Response from GHLR_21
    Response from GHLR_31
    Closing output file at  2-SEP-2014 11:05:11.
    %PMM-I-OUTCLOSE, Output file closed, writes to file switched off
    Output file opened at  2-SEP-2014 11:10:12.
    %PMM-I-OUTOPEN, Opened output file SMSC$ROOT:[LOG]SMSC_COUNTERS_GHLR.14090211
    PMS> SHOW CLASS GHLR /COUNT
    Response from GHLR_11
                                     Received messages: 11
                        Received messages and rejected: 0
                                  Transmitted messages: 12
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 42             
    Response from GHLR_21
                                     Received messages: 13
                        Received messages and rejected: 0
                                  Transmitted messages: 14
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 44             
    Response from GHLR_31
                                     Received messages: 7
                        Received messages and rejected: 0
                                  Transmitted messages: 8
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 38            
    PMS> RESET CLASS GHLR /COUNT
    Response from GHLR_11
    Response from GHLR_21
    Response from GHLR_31
    Closing output file at  2-SEP-2014 11:10:14.
    %PMM-I-OUTCLOSE, Output file closed, writes to file switched off

    Этот файл дописывается каждые пять минут блоками вида:

    Output file opened at  2-SEP-2014 11:05:10.
    %PMM-I-OUTOPEN, Opened output file SMSC$ROOT:[LOG]SMSC_COUNTERS_GHLR.14090211
    PMS> SHOW CLASS GHLR /COUNT
    Response from GHLR_11
                                     Received messages: 10
                        Received messages and rejected: 0
                                  Transmitted messages: 11
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 48             
    Response from GHLR_21
                                     Received messages: 12
                        Received messages and rejected: 0
                                  Transmitted messages: 13
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 49             
    Response from GHLR_31
                                     Received messages: 15
                        Received messages and rejected: 0
                                  Transmitted messages: 16
                       Messages that failed on sending: 0
                             Number of traced messages: 0
                Number of messages excluded from trace: 52             
    PMS> RESET CLASS GHLR /COUNT
    Response from GHLR_11
    Response from GHLR_21
    Response from GHLR_31
    Closing output file at  2-SEP-2014 11:05:11.

    Необходимо привести его к виду:

    2014-09-02 11:00:12 | GHLR | Received messages | GHLR_11 | 10
    2014-09-02 11:00:12 | GHLR | Received messages and rejecte | GHLR_11 | 0
    2014-09-02 11:00:12 | GHLR | Transmitted messages | GHLR_11 | 11	
    2014-09-02 11:00:12 | GHLR | Messages that failed on sending | GHLR_11 | 0
    2014-09-02 11:00:12 | GHLR | Number of traced messages | GHLR_11 | 0
    2014-09-02 11:00:12 | GHLR | Number of messages excluded from trace | GHLR_11 | 38
    2014-09-02 11:00:12 | GHLR | Received messages | GHLR_21 | 10
    2014-09-02 11:00:12 | GHLR | Received messages and rejecte | GHLR_21 | 0
    2014-09-02 11:00:12 | GHLR | Transmitted messages | GHLR_21 | 11	
    2014-09-02 11:00:12 | GHLR | Messages that failed on sending | GHLR_21 | 0
    2014-09-02 11:00:12 | GHLR | Number of traced messages | GHLR_21 | 0
    2014-09-02 11:00:12 | GHLR | Number of messages excluded from trace | GHLR_21 | 39
    2014-09-02 11:00:12 | GHLR | Received messages | GHLR_31 | 8
    2014-09-02 11:00:12 | GHLR | Received messages and rejecte | GHLR_31 | 0
    2014-09-02 11:00:12 | GHLR | Transmitted messages | GHLR_31 | 9	
    2014-09-02 11:00:12 | GHLR | Messages that failed on sending | GHLR_31 | 0
    2014-09-02 11:00:12 | GHLR | Number of traced messages | GHLR_31 | 0
    2014-09-02 11:00:12 | GHLR | Number of messages excluded from trace | GHLR_31 | 37
    2014-09-02 11:05:10 | GHLR | Received messages | GHLR_11 | 10
    2014-09-02 11:05:10 | GHLR | Received messages and rejecte | GHLR_11 | 0
    2014-09-02 11:05:10 | GHLR | Transmitted messages | GHLR_11 | 11	
    2014-09-02 11:05:10 | GHLR | Messages that failed on sending | GHLR_11 | 0
    2014-09-02 11:05:10 | GHLR | Number of traced messages | GHLR_11 | 0
    2014-09-02 11:05:10 | GHLR | Number of messages excluded from trace | GHLR_11 | 48
    2014-09-02 11:05:10 | GHLR | Received messages | GHLR_21 | 12
    2014-09-02 11:05:10 | GHLR | Received messages and rejecte | GHLR_21 | 0
    2014-09-02 11:05:10 | GHLR | Transmitted messages | GHLR_21 | 13	
    2014-09-02 11:05:10 | GHLR | Messages that failed on sending | GHLR_21 | 0
    2014-09-02 11:05:10 | GHLR | Number of traced messages | GHLR_21 | 0
    2014-09-02 11:05:10 | GHLR | Number of messages excluded from trace | GHLR_21 | 49
    2014-09-02 11:05:10 | GHLR | Received messages | GHLR_31 | 15
    2014-09-02 11:05:10 | GHLR | Received messages and rejecte | GHLR_31 | 0
    2014-09-02 11:05:10 | GHLR | Transmitted messages | GHLR_31 | 16	
    2014-09-02 11:05:10 | GHLR | Messages that failed on sending | GHLR_31 | 0
    2014-09-02 11:05:10 | GHLR | Number of traced messages | GHLR_31 | 0
    2014-09-02 11:05:10 | GHLR | Number of messages excluded from trace | GHLR_31 | 52
    .
    .
    .
    .

    Эксперты, помогите.

    4 сентября 2014 г. 8:04

Ответы

  • PowerShell:

    $log = Get-Content log.txt 
    $result = @()
    for($i=0;$i -lt $log.count;$i++) {
    	if($log[$i] -match "Output file opened at") {
    		$index = @()
    		$date = Get-Date ($log[$i] -replace "(Output file opened at\s+|\.)")
    		$date = $date.Tostring("yyyy-MM-dd HH:mm:ss")
    		while($log[$i] -notmatch "PMS> RESET CLASS GHLR /COUNT") {
    			$i++
    			if($log[$i] -match "Response from") {
    				$index += $i
    			}
    		}
    		for($x=0;$x -lt $index.count;$x++) {
    			$GHLR = $log[$index[$x]] -replace "Response from\s+"
    			$e = $index[$x+1]-1
    			if(!$index[($x+1)]) {
    				$e = $i-1
    			}
    			$log[($index[$x]+1)..$e] | Foreach {
    				$str = $_ -replace ":"," | $GHLR | "
    				$result += "{0} | GHLR |{1}" -f $date,$str.Trim()
    			}
    		}
    	}
    }	
    $result | Out-File result.txt

    • Помечено в качестве ответа Shurepsky 4 сентября 2014 г. 10:19
    4 сентября 2014 г. 9:34
    Отвечающий

Все ответы

  • PowerShell:

    $log = Get-Content log.txt 
    $result = @()
    for($i=0;$i -lt $log.count;$i++) {
    	if($log[$i] -match "Output file opened at") {
    		$index = @()
    		$date = Get-Date ($log[$i] -replace "(Output file opened at\s+|\.)")
    		$date = $date.Tostring("yyyy-MM-dd HH:mm:ss")
    		while($log[$i] -notmatch "PMS> RESET CLASS GHLR /COUNT") {
    			$i++
    			if($log[$i] -match "Response from") {
    				$index += $i
    			}
    		}
    		for($x=0;$x -lt $index.count;$x++) {
    			$GHLR = $log[$index[$x]] -replace "Response from\s+"
    			$e = $index[$x+1]-1
    			if(!$index[($x+1)]) {
    				$e = $i-1
    			}
    			$log[($index[$x]+1)..$e] | Foreach {
    				$str = $_ -replace ":"," | $GHLR | "
    				$result += "{0} | GHLR |{1}" -f $date,$str.Trim()
    			}
    		}
    	}
    }	
    $result | Out-File result.txt

    • Помечено в качестве ответа Shurepsky 4 сентября 2014 г. 10:19
    4 сентября 2014 г. 9:34
    Отвечающий
  • Большое человеческое спасибо;))) выручили.
    4 сентября 2014 г. 10:20
  • PowerShell:

    $log = Get-Content log.txt 
    $result = @()
    for($i=0;$i -lt $log.count;$i++) {
    	if($log[$i] -match "Output file opened at") {
    		$index = @()
    		$date = Get-Date ($log[$i] -replace "(Output file opened at\s+|\.)")
    		$date = $date.Tostring("yyyy-MM-dd HH:mm:ss")
    		while($log[$i] -notmatch "PMS> RESET CLASS GHLR /COUNT") {
    			$i++
    			if($log[$i] -match "Response from") {
    				$index += $i
    			}
    		}
    		for($x=0;$x -lt $index.count;$x++) {
    			$GHLR = $log[$index[$x]] -replace "Response from\s+"
    			$e = $index[$x+1]-1
    			if(!$index[($x+1)]) {
    				$e = $i-1
    			}
    			$log[($index[$x]+1)..$e] | Foreach {
    				$str = $_ -replace ":"," | $GHLR | "
    				$result += "{0} | GHLR |{1}" -f $date,$str.Trim()
    			}
    		}
    	}
    }	
    $result | Out-File result.txt

    Помогите еще в одной задаче. Необходимо в выводе в поле наименования метрики заменить пробелы на _. т.е вместо:

    2014-09-02 11:05:10 | GHLR | Number of messages excluded from trace | GHLR_31 | 52

    выводить:

    2014-09-02 11:05:10 | GHLR | Number_of_messages_excluded_from_trace | GHLR_31 | 52

     
    12 сентября 2014 г. 12:48