Reputation: 125
I am data mining a log file and exporting information successfully. What I am looking to do is put a few blank, empty, or some kind of line separator between each export within the data mining criteria the script finds. I currently have:
SET "_LogFile=C:\Test.log"
SET "_ResultFile=OutPut.txt"
SET "_MatchString_Begin=<Line Text="***********TEST************"
SET "_MatchString_End=</Report>"
SET "_Line#_Begin="
)
CALL :Main
( ENDLOCAL
EXIT/B
)
:Main
CLS
IF EXIST "%_ResultFile%" (
DEL /F /Q "%_ResultFile%"
)
COLOR 0F
ECHO.&ECHO.===== Processing =====&ECHO.
FOR /F "Delims=[]" %%# IN ('
Find /N "%_MatchString_Begin:"=""%" "%_LogFile%" ^| FIND "["
') DO (
ECHO. Found Match On Line %%#
SET /A "_Line#_Begin=%%#-1"
CALL :Output
)
COLOR 20
ECHO.&ECHO.===== Completed! =====
FOR /F tokens^=2^ delims^=^" %%I in ('type "%_ResultFile%" ^& ^> "%_ResultFile%" rem/') do (
>> "%_ResultFile%" echo(%%I
)
REN %_ResultFile% Trythis.csv
START TryThis.csv
GOTO :EOF
:Output
FOR /F "SKIP=%_Line#_Begin% Tokens=* usebackq" %%_ IN (
"%_LogFile%"
) DO (
ECHO(%%_
ECHO("%%_" | FIND /I "%_MatchString_End%" >NUL&&(
GOTO :EOF
)
)>>"%_ResultFile%"
GOTO :EOF
In my export I get:
AAA
AAA
AAA
BBB
BBB
BBB
CCC
CCC
CCC
Would be nice to see:
AAA
AAA
AAA
---
BBB
BBB
BBB
---
CCC
CCC
CCC
A is one block that is exported, B is another Block Exported, C is another block exported. They currently all merge line after line. Would be nice to a separator in between each block exported.
Any suggestions would be MUCH appreciated!
Upvotes: 1
Views: 182
Reputation: 2135
Add this line:
echo/>>"%_ResultFile%"
immediately after this line:
CALL :Output
inside the for
loop.
But I'm not sure what this for
loop even does, so it could mess it up:
FOR /F tokens^=2^ delims^=^" %%I in ('type "%_ResultFile%" ^& ^> "%_ResultFile%" rem/') do (
>> "%_ResultFile%" echo(%%I
)
Upvotes: 0
Reputation: 2135
I think you just need to put it in the :output section like this:
:Output
FOR /F "SKIP=%_Line#_Begin% Tokens=* usebackq" %%_ IN ("C:\Test.log") DO (
ECHO(%%_
ECHO("%%_" | FIND /I "%_MatchString_End%" >NUL&&ECHO(&&(GOTO :EOF)
)>>"%_ResultFile%"
Your full code, changing a bunch of things the way I would more likely do them. I know Mofi really dislikes echo.
-- but I work with computers that I have control of and I do not worry about the tiny chance of problems. If you care about it, you can replace all of the echo.
with echo/
SET "_MatchString_Begin=<Line Text="**TEST**"
SET "_MatchString_End=</Report>"
SET "_Line#_Begin="
ECHO.
ECHO ===== Processing =====
ECHO.
FOR /F "Delims=[]" %%a IN ('Find /N "%_MatchString_Begin:"=""%" "C:\Test.log" ^| FIND "["') DO (
ECHO Found Match On Line %%a>>"%_ResultFile%"
SET /A "_Line#_Begin=%%a-1"
rem CALL :Output I do not understand what this line is doing here.
rem :Output I also do not understand what this line is doing here.
FOR /F "SKIP=%_Line#_Begin% Tokens=* usebackq" %%b IN ("C:\Test.log") DO (
ECHO.%%b>>"%_ResultFile%"
rem the next line is the blank line if you want it after every line in test.log
ECHO.>>"%_ResultFile%"
rem the next line also adds a blank line after finding the ending matching string
ECHO.%%b^| FIND /I "%_MatchString_End%" >NUL && (
rem what to do when we match the end string
rem blank line here if this is where you want it
echo.>>"%_ResultFile%"
GOTO :EOF
)
)
)
GOTO :EOF
I find that redirecting every line you want into the output file is more understandable than redirecting the entire output of a for
statement to a file. It does lead to repeated code -- but you have the flexibility of redirecting to multiple files or just echoing to the console.
Also, I still don't think this is your actual code. I had to add parentheses here and there. Perhaps the :output section was supposed to be outside the initial for
loop ??
Here's what I think your code is actually supposed to be:
SET "_MatchString_Begin=<Line Text="**TEST**"
SET "_MatchString_End=</Report>"
SET "_Line#_Begin="
ECHO.
ECHO ===== Processing =====
ECHO.
FOR /F "Delims=[]" %%a IN ('Find /N "%_MatchString_Begin:"=""%" "C:\Test.log" ^| FIND "["') DO (
ECHO Found Match On Line %%a
SET /A "_Line#_Begin=%%a-1"
CALL :Output
echo.>>"%_ResultFile%"
)
goto :eof
:Output
FOR /F "SKIP=%_Line#_Begin% Tokens=* usebackq" %%b IN ("C:\Test.log") DO (
ECHO.%%b>>"%_ResultFile%"
ECHO.%%b| FIND "%_MatchString_End%" >NUL && GOTO :EOF
)
GOTO :EOF
Ultimately though, I wouldn't loop through the file over and over again like that. This is what I'd do:
UNTESTED Could easily be minor errors like missing a ^
.
@echo off
SET "_MatchString_Begin=<Line Text="**TEST**"
SET "_MatchString_End=</Report>"
SET "_Line#_Begin="
:: output_flag is used to decide if the lines should be output or not
SET "output_flag=N"
cls
ECHO.
ECHO ===== Processing =====
ECHO.
FOR /F "usebackq tokens=*" %%a IN ("C:\Test.log") DO (
ECHO.%%a|find "%_MatchString_Begin:"=""%" && set "output_flag=Y"
if "!output_flag!"=="Y" echo.%%a>>"%_ResultFile%"
echo.%%a|find "%_MatchString_End%" >nul &&echo.>>"%_ResultFile%"&&set "output_flag=N"
)
Upvotes: 0