Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  | Правила  

Компьютерный форум OSzone.net » Программирование, базы данных и автоматизация действий » Скриптовые языки администрирования Windows » Любой язык - [решено] Помогите распарсить log-файл.

Ответить
Настройки темы
Любой язык - [решено] Помогите распарсить log-файл.

Новый участник


Сообщения: 5
Благодарности: 0

Профиль | Отправить PM | Цитировать


Всем привет!

Имеется лог-файл вида:
Код: Выделить весь код
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
.
.
.
.
В программировании не силен. Помогите, в долгу не останусь.

Отправлено: 11:52, 04-09-2014

 

Ветеран


Сообщения: 874
Благодарности: 575

Профиль | Отправить PM | Цитировать


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

Последний раз редактировалось Georgio, 04-09-2014 в 19:52.

Это сообщение посчитали полезным следующие участники:

Отправлено: 17:27, 04-09-2014 | #2



Для отключения данного рекламного блока вам необходимо зарегистрироваться или войти с учетной записью социальной сети.

Если же вы забыли свой пароль на форуме, то воспользуйтесь данной ссылкой для восстановления пароля.


Новый участник


Сообщения: 5
Благодарности: 0

Профиль | Отправить PM | Цитировать


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

Отправлено: 10:24, 05-09-2014 | #3



Компьютерный форум OSzone.net » Программирование, базы данных и автоматизация действий » Скриптовые языки администрирования Windows » Любой язык - [решено] Помогите распарсить log-файл.

Участник сейчас на форуме Участник сейчас на форуме Участник вне форума Участник вне форума Автор темы Автор темы Шапка темы Сообщение прикреплено

Похожие темы
Название темы Автор Информация о форуме Ответов Последнее сообщение
2003/XP/2000 - Распарсить XLS и получить интересующие данные во внешний файл bagfinder Microsoft Office (Word, Excel, Outlook и т.д.) 14 29-01-2014 14:41
Почта - syncback не отправляет на email log файл mitkaua Программное обеспечение Windows 0 26-01-2012 13:17
Ошибка - файл или каталог c/windows/system32/config/system.log поврежден и не может быть прочи zagruzkaaa Microsoft Windows 2000/XP 1 23-01-2010 17:27
setupact.log - при установке появляется много: Не удается найти указанный файл HWG Автоматическая установка Windows 2000/XP/2003 2 21-10-2005 14:16
Файл start.log Al ex Защита компьютерных систем 2 25-01-2005 12:52




 
Переход