[Batch] Working with a long single line

Cooler master Hyper 212 plus cpu cooler
November 23, 2011 at 08:23:06
Specs: Windows 7 Ultimate x64 , 4 GB
I have a log file that I would like to parse. This file is XML formatted, but for whatever reason, when opened with anything other than the Microsoft Service Trace Viewer, it appears all on one long line. Within this file, is a 32 byte hexadecimal string I need to parse out, appearing an unknown amount of times. I just need to know how to iterate through this line, and place a CRLF after certain parts so I can parse it out with a for loop. If that doesn't seem logical, then I guess I need a way to iterate through this file, splitting it up at token 7, 14, 21, etc, for every occurnence of the 32 byte string I need.

Here is the log file. Keep in mind that the number of times that 32 byte hex string appears will be different everytime.

http://pastebin.com/7SvaUtLP

If you need anymore info, let me know.

Thanks,

Tony


See More: [Batch] Working with a long single line

Report •


#1
November 24, 2011 at 21:51:33
And the 32 byte string is???


Please come back & tell us if your problem is resolved.


Report •

#2
December 21, 2011 at 12:29:54
This was the script I came up with, works perfectly.


@echo off & setlocal enableextensions
set count=0
set out=duid_fix.sql
set s=regex.replace(wscript.stdin.readall,">"+vbCRLF)
if exist %out% del %out%
if "%1"=="/s" (
if "%2"=="" (
goto :_help
) else (
if "%2"=="/?" (
goto :_help
)
)
if not exist "%2" (
echo File not found. && goto :eof
)
)
if "%1"=="" goto :_help
if "%1"=="/?" goto :_help
if "%1"=="/s" goto :_safe
if "%1"=="/s" (
if not exist %2 echo File not found. && goto :eof
) else (
if not exist %1 echo File not found. && goto :eof
)
for /f "tokens=1 delims=<>" %%l in ('type %1') do (
set t=%%l
)
for /f "tokens=1 delims= " %%y in ('echo %t%') do (
set g=%%y
)
if not %g%==E2ETraceEvent (
echo Invalid log file.
goto :eof
)
call :_vbs
cscript /nologo _.vbs < "%1" > "_.xml"
for /f "tokens=2 delims=." %%i in (
'type _.xml ^| findstr "document"'
) do echo %%i>> _.tmp
)
for /f "tokens=9 delims= " %%a in ('type _.tmp') do (
call :_trim %%a
)
call :_delDup
for /f "tokens=*" %%i in (%out%) do (
set /a count=count+1
)
echo.&&echo Found %count% DUID's.
for %%z in (_.xml _.tmp _.vbs) do del %%z 2>nul
echo.&&echo Done.
goto :eof
:_trim
set var=%*
if not "%var:~31,1%"=="" (
echo DELETE FROM dbo.Annot WHERE duid='%1'>>%out%
) else (
echo.&&echo Using SELECT instead of DELETE for DUID %1.
echo.&&echo You may have to manually delete that DUID.
echo SELECT ^* FROM dbo.Annot WHERE duid='%1'>>%out%
)
goto :eof
:_safe
echo.&&echo Using Safe Mode.
echo SQL script will use SELECT instead of DELETE.
for /f "tokens=1 delims=<>" %%l in ('type %2') do (
set t=%%l
)
for /f "tokens=1 delims= " %%i in ('echo %t%') do (
set x=%%i
)
if not %x%==E2ETraceEvent (
echo Invalid log file.
goto :eof
)
call :_vbs
cscript /nologo _.vbs < "%2" > "_.xml"
for /f "tokens=2 delims=." %%i in (
'type _.xml ^| findstr "document"'
) do echo %%i>> _.tmp
)
for /f "tokens=9 delims= " %%a in (
'type _.tmp'
) do call :_trim2 %%a
)
call :_delDup
for /f "tokens=*" %%i in (%out%) do (
set /a count=count+1
)
echo.&&echo Found %count% DUID's.
for %%z in (_.xml _.tmp _.vbs) do del %%z 2>nul
echo.&&echo Done.
goto :eof
:_trim2
set var=%*
echo SELECT ^* FROM dbo.Annot WHERE duid='%1'>>%out%
goto :eof
:_delDup
@echo off > outfile
for /f "tokens=* delims= " %%a in (%out%) do (
find "%%a" < outfile > nul
if errorlevel 1 echo %%a>> outfile
)
move outfile %out% > nul
goto :eof
:_vbs
>_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=true
>>_.vbs echo regex.pattern=">"
>>_.vbs echo wscript.stdOut.write %s%
goto :eof
:_help
echo Builds a SQL script based on DUID's in an svclog file.
echo.&&echo %~n0 [/s] [drive:][path]filename
echo.&&echo /s Use Safe-Mode. Uses SELECT instead of DELETE.
goto :eof


Report •
Related Solutions


Ask Question