cmd - Using a batch file to find text in a file, then cleaning it up, saving to a file and a variable -


ok have searched , searched days , cant find works. if missed sorry.

my problem: have text file source code web page. goal search text file , find. "below lines around want"

<b>public</b> </a> </td> <td></td> <td class="b"> 705330 </td> <tr> <tr> <td> 

(there lot more source code other numbers kind of same way public unique. below unique(not numbers) right thought more matched better)

<td class="b"> 705330 </td> 

i trying numbers(so have remove numbers), number change rest doesn't. want save numbers file .txt(just numbers) (first line , on writes previous save) , assigns variable can compare previous run commands.

like comparing new(variable) old .txt , doing something.

i got rest of down, cant figure out. ive tried find, findstr , every forum trying fine work me. couldn't searched string variable echoed 30 lines or did nothing.

i appreciate help, in advance

okay -- quite tricky...

in batch, applications rather rigit, dealing such highly flexible text (html) files challenging; tried though...

the batch script following below assumes that:

  • one (text) file given command line argument;
  • the "<b>public</b>" field unique (this not checked);
  • the "<b>public</b>" field , "class=" token case-sensitive both;
  • there "class=" token appearing after "<b>public</b>" field;
  • the number of interest appears somewhere after "class=" token (optionally after);
  • the number of interest given in own separate line;

so let's go (explanation within remarks):

@echo off setlocal enabledelayedexpansion  rem set constant holding exact appearance of "public" field; rem ^ escapes < , > 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 non-empty value if want target value right after "class" token set class_glue=  rem initialise variable holds line number of "public" field set linepublic=0 rem clear variable set "class" token found set foundclass= rem clear variable hold resulting (numeric) target value set fieldvalue=  rem check command line argument being given if "%1"=="" (echo no file given^^!& exit /b)  rem search unique "public" field, return found line, prefix line number; rem `2> nul` portion avoids displaying `findstr` errors in case of input lines > 8192 chars.; rem wrapped-around `for /f` retrieves line number only, stored in %linepublic% /f "delims=:" %%l in ('type "%~1" ^| findstr /n /l "%public_tag%" 2^> nul') set linepublic=%%l rem if no "public" field found, terminate batch script if %linepublic% equ 0 (echo file not contain field "%public_tag%"^^!& exit /b) rem starting @ line number %linepublic%, go through each line /f "usebackq skip=%linepublic% delims=" %%f in ("%~1") ( rem check if %foundclass% has been set in (one of the) previous `for` iteration(s) if defined foundclass ( rem "class" token found previously, check if target value has been found if not defined fieldvalue ( rem no target value available yet, check if current line contains decimal digits echo."%%f" | findstr /r "^\"[0-9][0-9]*\" \$" > nul rem if errorlevel 0 (below 1), current line constitutes 1 numeric value, store it; rem `call` statement necessary avoid syntax errors due < , > in line text %%f if not errorlevel 1 ((call set fieldvalue=%%f) & goto :fine) else ( rem if want target value right after "class" token, %class_glue% must set: if defined class_glue (echo no number follows "%class_prop%" token^^!& exit /b) ))) rem search current line "class" token; errorlevel 0 if found echo."%%f" | findstr /l "%class_prop%" > nul rem if errorlevel below 1, indicate setting %foundclass%, checked in next `for` iteration if not errorlevel 1 set foundclass=true ) & rem next %%f :fine rem compound statement makes %fieldvalue% survive `setlocal`/`endlocal` block endlocal & set fieldvalue=%fieldvalue% echo.%fieldvalue% 

this works @ least text file sample...

hint: if want numerical value expected immediately after "class=" token, set variable (constant) class_glue in code valid non-empty value.

so accomplish task of storing number text file, need enter:

above_batch_script_name.bat input_html_text_file.txt > output_text_file.txt 

remark: since batch scripts not powerful in string operation , manipulation, not best choice kind of challenge. anyway, hope helps...


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -