Reputation: 25
Ok I have searched and searched for days and cant find anything that works. If I missed something i am truly sorry.
My problem: I have a text file with the source code from a web page. My goal is to search the text file and find. "below is the lines around what i want"
<b>public</b>
</a>
</td>
<td></td>
<td class="b">
705330
</td>
<tr>
<tr>
<td>
(there is a lot more source code with other numbers kind of the same way but the public is unique. Below is unique(not the numbers) right now but i thought the more matched the better)
<td class="b">
705330
</td>
I am trying to get those numbers(so have to remove everything but the numbers), the number change but the rest doesn't. I want to save the numbers to a file .txt(Just the numbers) (first line and over writes the previous save) and assigns to a variable so it can compare to the previous then run some commands.
Like comparing the new(variable) to the old .txt and doing something.
I got the rest of it down, just cant figure this out. ive tried find, findstr and every forum trying to fine something that would work for me. But couldn't get the searched string into a variable it just echoed about 30 lines or did nothing.
I appreciate any help, Thanks in advance
Upvotes: 2
Views: 305
Reputation: 34949
Okay -- this is quite tricky...
In batch, most applications are rather rigit, so dealing with such highly flexible text (HTML) files is challenging; I tried it though...
The batch script following below assumes that:
So let's go (explanation within remarks):
@echo off
setlocal EnableDelayedExpansion
rem set constant holding exact appearance of "public" field;
rem ^ escapes < and > which would otherwise constitute (unintended) redirections
set PUBLIC_TAG=^<b^>public^</b^>
rem set constant holding exact appearance of "class" token
set CLASS_PROP=class=
rem set constant to non-empty value if you want the target value to be right after the "class" token
set CLASS_GLUE=
rem initialise variable that holds line number of "public" field
set LinePublic=0
rem clear variable that is set as soon as "class" token is found
set FoundClass=
rem clear variable that will hold resulting (numeric) target value
set FieldValue=
rem check for command line argument being given
if "%1"=="" (echo No file given^^!& exit /B)
rem search for unique "public" field, return found line, prefix with line number;
rem the `2> nul` portion avoids displaying any `findstr` errors in case of input lines > 8192 chars.;
rem wrapped-around `for /F` retrieves line number only, stored in %LinePublic%
for /F "delims=:" %%L in ('type "%~1" ^| findstr /N /L "%PUBLIC_TAG%" 2^> nul') do set LinePublic=%%L
rem if no "public" field found, terminate batch script
if %LinePublic% equ 0 (echo File does not contain field "%PUBLIC_TAG%"^^!& exit /B)
rem starting at line number %LinePublic%, go through each line
for /F "usebackq skip=%LinePublic% delims=" %%F in ("%~1") do (
rem check if %FoundClass% has been set in (one of the) previous `for` iteration(s)
if defined FoundClass (
rem "class" token found previously, so check if target value has already been found
if not defined FieldValue (
rem no target value available yet, so check if current line contains decimal digits only
echo."%%F" | findstr /R "^\"[0-9][0-9]*\" \$" > nul
rem if ErrorLevel is 0 (below 1), current line constitutes one numeric value, so store it;
rem the `call` statement is necessary to avoid syntax errors due to < and > in line text %%F
if not ErrorLevel 1 ((call set FieldValue=%%F) & goto :FINE) else (
rem if you want the target value to be right after the "class" token, %CLASS_GLUE% must be set:
if defined CLASS_GLUE (echo No number follows "%CLASS_PROP%" token^^!& exit /B)
)))
rem search current line for "class" token; ErrorLevel is 0 if found
echo."%%F" | findstr /L "%CLASS_PROP%" > nul
rem if ErrorLevel is below 1, indicate by setting %FoundClass%, checked in next `for` iteration
if not ErrorLevel 1 set FoundClass=True
) & rem next %%F
:FINE
rem this compound statement makes %FieldValue% to survive `setlocal`/`endlocal` block
endlocal & set FieldValue=%FieldValue%
echo.%FieldValue%
This works at least for your text file sample...
Hint: If you want the numerical value to be expected immediately after the "class=" token, set the variable (constant) CLASS_GLUE
in the code to any valid non-empty value.
So finally to accomplish the task of storing the number into a text file, you need to enter:
above_batch_script_name.bat input_html_text_file.txt > output_text_file.txt
Remark: Since batch scripts are not powerful in string operation and manipulation, they are probably not the very best choice for this kind of challenge. Anyway, I hope this helps...
Upvotes: 1