Компьютерный форум OSzone.net  

Компьютерный форум OSzone.net (http://forum.oszone.net/index.php)
-   Скриптовые языки администрирования Windows (http://forum.oszone.net/forumdisplay.php?f=102)
-   -   [решено] Помогите распарсить log-файл. (http://forum.oszone.net/showthread.php?t=287466)

shurepsky 04-09-2014 11:52 2398236

Помогите распарсить log-файл.
 
Всем привет!

Имеется лог-файл вида:
Код:

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
.
.
.
.

В программировании не силен. Помогите, в долгу не останусь.

Georgio 04-09-2014 17:27 2398383

shurepsky, здравствуйте. Пакетный файл (файл с расширением ".bat" или с расширением ".cmd"):


Код:

@echo off
setlocal enabledelayedexpansion

set "input_file=example.log"
set "output_file=new.log"

for %%a in (JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC) do (
 set /a count+=1
 set %%a=0!count!
 )
(
for /f "tokens=1,2,3,4,5 delims=-.:" %%a in ('
 findstr /il /c:"Output file opened at" /c:Response /c:Received /c:Transmitted /c:Messages /c:Number "%input_file%"
') do (
 echo %%a| findstr /bilc:"Output file opened at">nul&& (
 for %%f in (%%a) do (
  set day=0%%f
  )
 for /f "tokens=1,2" %%f in ("%%c") do (
  set date_and_time=%%f-!%%b:~-2!-!day:~-2! %%g:%%d:%%e
  )
 )|| (
 echo %%a| findstr /bilc:"Response from GHLR_">nul&& (
 for %%f in (%%a) do (
  set GHLR=%%f
  )
 )|| (
 for /f "tokens=*" %%f in ("%%a") do (
  echo !date_and_time! ^| GHLR ^| %%f ^| !GHLR! ^|%%b
  )
 )
 )
 )
)>"%output_file%"

endlocal
exit /b


shurepsky 05-09-2014 10:24 2398645

Спасибо большое. Вот еще реализация на 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



Время: 16:13.

Время: 16:13.
© OSzone.net 2001-