[SOLVED] Batch to Parse tags from .log file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

[SOLVED] Batch to Parse tags from .log file

#1 Post by Dos_Probie » 09 Mar 2014 15:34

I have never had the need to parse files using a batch, have googled around but cannot seem to find
any appropriate documention on this (found something on perl but rather stick to a batch) and now need some direction please.
This is an snippet example of text in my Results.log file (total of 28 lines) below.

<td><label>IP Address</label></td>
<td><label>90.167.72.49</label></td>
<td><label for="chkCountry">Country</label></td>
<td><label for="chkCountry">UNITED STATES</label></td>
<td><label for="chkNetSpeed">Net Speed</label></td>
<td><label for="chkNetSpeed">DSL</label></td>

Would like to parse or extract the End tags </label></td> (which are all the same on each line)
and then the Start tags (which vary per info) <td><label>, <td><label for="chkCountry">, <td><label for="chkNetSpeed"> etc.
When done parsing out the tags then update the Results.log or just del old and create new EndResult.txt file.
Can this be done? If so then Thanks for any help or info on this..DP
Last edited by Dos_Probie on 11 Mar 2014 16:21, edited 2 times in total.

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Batch to Parse tags from .log file

#2 Post by Squashman » 09 Mar 2014 16:15

Most likely can be done in pure batch but MS made a utility for things like this. Although I am sure REPL and FINDREPL would work quite nicely.
http://www.microsoft.com/en-us/download ... x?id=24659

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch to Parse tags from .log file

#3 Post by Dos_Probie » 09 Mar 2014 16:40

Thanks for the info Squashman!, I failed to mention that my OS will either be Windows 7 or 8 and the System requirements
for Log Parser is "only compatible with the Windows® 2000, Windows® XP Professional, and Windows ServerTM 2003 operating systems." , Have SSED which was able to gleam 248 lines down to 28 but don't see where it has the ability to parse, so pure batch would probably work, just need pointing in right direction..DP

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch to Parse tags from .log file

#4 Post by Dos_Probie » 09 Mar 2014 17:26

I got this code to find a specified text now just need to figure out how to remove it and ability to add multiple specified text files..

Code: Select all

@echo off
:: Search for specified text in log file
set "log=IP.RESULTS.log"

findstr /m "</label></td>" %log%
if %errorlevel%==0 (
echo.
echo Found!
) else (
echo No matches found
)
pause>nul

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch to Parse tags from .log file

#5 Post by foxidrive » 09 Mar 2014 19:00

<td><label>IP Address</label></td>
<td><label>90.167.72.49</label></td>
<td><label for="chkCountry">Country</label></td>
<td><label for="chkCountry">UNITED STATES</label></td>
<td><label for="chkNetSpeed">Net Speed</label></td>
<td><label for="chkNetSpeed">DSL</label></td>

Would like to parse or extract the End tags </label></td> (which are all the same on each line)
and then the Start tags (which vary per info) <td><label>, <td><label for="chkCountry">, <td><label for="chkNetSpeed"> etc.
When done parsing out the tags then update the Results.log or just del old and create new EndResult.txt file.


Show us what you need to get from the above.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch to Parse tags from .log file

#6 Post by Dos_Probie » 09 Mar 2014 21:35

Thanks for the Reply Foxi, here is what I Need to KEEP from my snippet with spacing for readability...
IP Address
90.167.72.49

Country
UNITED STATES

Net Speed
DSL

also this is what I have so far, but its slow and the special characters ( </ ) are a issue with enabledelayedexpansion..

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "log=RESULTS.log"
set "log2=RESULTS_Rev.txt"

FOR /F "usebackq delims=" %%G IN ("%log%") DO (
Set "line=%%G" & echo !line:label for=!
)>>"%log2%"

Aacini
Expert
Posts: 1615
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: Batch to Parse tags from .log file

#7 Post by Aacini » 09 Mar 2014 21:43

Code: Select all

@echo off
setlocal

set "log=RESULTS.log"
set "log2=RESULTS_Rev.txt"

(FOR /F "usebackq tokens=3 delims=<>" %%G IN ("%log%") DO (
   echo %%G
)) > "%log2%"


Antonio
Last edited by Aacini on 09 Mar 2014 21:44, edited 1 time in total.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Batch to Parse tags from .log file

#8 Post by foxidrive » 09 Mar 2014 21:44

Using Dave Benham's repl.bat

Code: Select all

@echo off
type "file.htm"|repl ".*<td><label.(.*)</label>.*" "$1" i |repl "^for.*\q>" "" xi |repl "^(IP Address|Country|Net Speed)$" "\r\n$1" x
pause


Output:


IP Address
90.167.72.49

Country
UNITED STATES

Net Speed
DSL
Press any key to continue . . .




This uses a helper batch file called `repl.bat` - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.

dbenham
Expert
Posts: 2283
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch to Parse tags from .log file

#9 Post by dbenham » 10 Mar 2014 05:44

Here is a simpler and more generic way to do it with REPL.BAT.

Code: Select all

type results.log|repl "<.*?>" ""|repl "(.*\n.*\n)" "$1\r\n" mx >results.log.new
move /y results.log.new results.log >nul

The first REPL removes all tags. The second REPL inserts an extra linefeed every other line.

Edit
Here is a slightly simpler version (saves 2 characters :wink: )

Code: Select all

type results.log|repl "<.*?>" ""|repl ".*\n.*\n" "$&\r\n" mx >results.log.new
move /y results.log.new results.log >nul

The original post of REPL.BAT is at viewtopic.php?f=3&t=3855


Dave Benham
Last edited by dbenham on 10 Mar 2014 07:40, edited 2 times in total.

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: Batch to Parse tags from .log file

#10 Post by Squashman » 10 Mar 2014 06:30

Dos_Probie wrote:Thanks for the info Squashman!, I failed to mention that my OS will either be Windows 7 or 8 and the System requirements
for Log Parser is "only compatible with the Windows® 2000, Windows® XP Professional, and Windows ServerTM 2003 operating systems." , Have SSED which was able to gleam 248 lines down to 28 but don't see where it has the ability to parse, so pure batch would probably work, just need pointing in right direction..DP

How often have you bought a piece of software well before an operating system came out and it still worked 10 years later when a new operating system comes out.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Batch to Parse tags from .log file

#11 Post by Dos_Probie » 10 Mar 2014 07:31

@Squashman, your right just cause it says it won't work a lot of times it will, anyway thanks for the input
and big Thanks to Dave and Foxi for the replace.bat (didn't know about that one) and code, I will be adding this
to my main batch code and then report back with the results and a download if anyone is interested. Thanks again
DP :D
P.S (Thanks also to Aacini and penpen for the input as well!)

EDIT:
OK, got this working 100% now! so big thanks again for all the help on this and the info on the repl.bat file as well, below is
a summary of the Geo ISP output which reads off a java script file to a specified geolocation service for your local machine
then uses the ssed.exe file to parse all of the .xml information from 445 lines down to only 28 lines then uses the Dos Tips file
repl.bat to pull out all the code tags, taking less that 15 sec to run, The file size is only 113kb and is uploaded on a cloud account,
if anyone is interested in trying this out just pm me for the d/l link..Thanks again DP :D

Sample Summary:
IP Address
90.167.72.49

Country
UNITED STATES

Region & City
FLORIDA, NAPLES

Latitude & Longitude
+20.092056, -61.719187

ZIP Code
34116

ISP
COMCAST CABLE COMMUNICATIONS HOLDINGS INC

Domain
COMCAST.NET

Time Zone
-05:00

Net Speed
DSL

IDD Code & Area Code
+1237

Weather Station
NAPLES (USFL0338)

Mobile MCC, MNC, & Carrier name
-

Elevation
2

Usage Type
ISP

========== END OF ISP SUMMARY - [Tue 03/11/2014 @ 05:06 PM] ==========
Last edited by Dos_Probie on 11 Mar 2014 19:19, edited 2 times in total.

penpen
Expert
Posts: 1723
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch to Parse tags from .log file

#12 Post by penpen » 11 Mar 2014 00:36

If you are have to extract multiple informations from tagged data, you also may use JScript to perform xml/xslt:
"test.bat":

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
set "empty="

:: The name of the root elements doesn't matter, there must be only one
:: Assumed this is a default text file
> result.xml echo(^<root^>
copy result.xml /B + result.log /B result.xml
>> result.xml echo(^</root^>

(
   echo(^<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"^>
   echo(
   echo(^<xsl:output method="text" encoding="UTF-8"/^>
   echo( ^<xsl:template match="label"^>
   echo(  ^<xsl:copy-of select="."/^>^<xsl:text^>^&#13;^&#10;^</xsl:text^>
   echo( ^</xsl:template^>
   echo(^</xsl:transform^>
) > resultExtract.xsl

call xslt.bat "result.xml" "resultExtract.xsl" "result.txt"

:: if result.log has a root element, you may use it without another temp file
::xslt.bat "result.log" "resultExtract.xsl" "result.txt"

:: i haven't deleted the files for debug reasons
endlocal
goto :eof

"xslt.bat":

Code: Select all

@if (true==false) then /*
@echo off
cscript //nologo //e:JScript "%~f0" %*
exit /b %errorlevel%
*/
@end


function loadXmlFile (strFileName) {
   var xmlObject = xmlObject = new ActiveXObject ("msxml2.DOMDocument.6.0");      // try instanciating MSXML 6.0 object
   if (xmlObject == null) {
      xmlObject = new ActiveXObject  ("Msxml2.DOMDocument.3.0");         // try instanciating MSXML 3.0 object
      if (xmlObject == null) {
         WScript.echo ("Error: Could not instanciate MSXML 3.0/6.0.");      // echo error hint
         return null;
      }
   }

   xmlObject.async = false;                        // disable asynchronuous reading
   xmlObject.validateOnParse = false;                     // disable parser validating document
   xmlObject.load (strFileName);                        // load file content
   if (xmlObject.parseError.errorCode != 0) {
      WScript.Echo ("Parse error: " + xmlObject.parseError.reason);
      return null;
   }

   return xmlObject;
}

function xslt (sourceXml, sourceXslt, target) {
   var objFileSystem = new ActiveXObject ("Scripting.FileSystemObject");         // create file system object handle
   var objXML = loadXmlFile (sourceXml);                     // create input xml file handle
   var objXSL = loadXmlFile (WScript.arguments (1));               // create input xsl file handle

   try {
      strResult = objXML.transformNode (objXSL);               // creating output txt file content
      var objOutFile = objFileSystem.createTextFile (WScript.arguments (2), true);   // create output txt file handle
      objOutFile.write (strResult);                     // writing content to file
      objOutFile.close ();                        // flush and close txt file
   } catch (exception) {
      WScript.echo ("XSL Transformation failed.");
   }
}

if (WScript.Arguments.Unnamed.Length != 3) {
   WScript.echo ("Usage: xslt <input xml file> <input xsl file> <output txt file>");
   WScript.quit (1);
} else {
   xslt (WScript.arguments (0), WScript.arguments (1), WScript.arguments (2));
   WScript.quit (0);
}

penpen

Edit: Added a missing "call" in test.bat
Last edited by penpen on 25 Jun 2015 16:56, edited 1 time in total.

pieh-ejdsch
Posts: 204
Joined: 04 Mar 2014 11:14
Location: germany

Re: [SOLVED] Batch to Parse tags from .log file

#13 Post by pieh-ejdsch » 12 Mar 2014 05:46

To extract text from HTML or XML you can use this simple pure Batchscript http://www.administrator.de/forum/html-dateien-nach-bestimmten-strings-auslesen-und-in-textdatei-speichern-180748.html#comment-733852

Code: Select all

@echo off&setlocal
pushD "%~dp0"
if "%~1" equ "" rd & goto :eof
if "%~1" neq "/?" if /i "%~1" neq "/help" goto :NoHelp
Echo Seperate Ausgabe von Code und Text ^(Text unformatiert^)
Echo(
Echo Syntax: "%~n0" [Laufwerk:][Pfad]Dateiname
Echo(
Echo HTML-Code Extractor nach "Dateiname.Code.txt"
Echo HTML-Text Extractor nach "Dateiname.txt"
Echo(
pause&goto :eof
:NoHelp
if not exist "%~1" echo Datei "%~1" nicht gefunden.& exit /b1
set "File=%~1"
Type nul>"%File%.Code.txt"
type nul>"%File%.TXT"
set /a Nr =0

:startNLine
set /a Nr +=1
for /f "usebackq delims=" %%i in ("%File%") do (set /a Nr+=1
   set /a Index =0
   set "Line=%%i"
   call :start
)
set HText&pause&goto :eof

:start

set /a Index +=1
set "Line1="
set "Text="
setlocal enabledelayedexpansion

for /f "tokens=1,* delims=<" %%i in (" !Line!") do endlocal&(if not "%%i" == " " set "Line1=%%i"
   set "Line=<%%j"
)

if not defined Line1 goto :NoLine1
setlocal enabledelayedexpansion
set "Text=!Line1:~1!"
(
for /f "eol= tokens=* delims=" %%i in ("!Text!") do endlocal&set "Text=%%i"
)||endlocal
:NoLine1
set "Line2="
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=>" %%i in (" !Line!") do endlocal&(if not "%%i" == " " set "Line2=%%i>"
   set "Line=%%j"
)

setlocal enabledelayedexpansion
set "Line2=!Line2:~1!"
(
for /f "eol= tokens=* delims=" %%i in ("!Line2!") do endlocal&if not "%%i" == "<>" (
   set "HTag:%Nr%:%Index%=%%i"
   echo HTMCode   : %%i
)>>"%File%.Code.txt"
)||endlocal

:NoLine

setlocal enabledelayedexpansion
(
for /f "eol= tokens=* delims=" %%i in ("!Text!") do (endlocal
   echo\%%i
   set "HText:%Nr%:%Index%=%%i"
   set "Text="
)>>"%File%.TXT"
)||endlocal

::hier müsste dann die Extrabehandlung herein

if defined Line (goto :Start) else goto :eof


This splits: for example
a HTML file to one Text file and one HTML-Code file
usage:
Batch.cmd "File-with-code-tags.ext"

Phil

Post Reply