Batch script to find a pattern and put in variable

Viewed 47

Currently I am trying to extract the most important information from a .html file with a batch script. To be more specific, I need to print something that I will write as ї, so ї - is the information I need. The script must find a pattern <a href="їїї">її</a> and get їїї and її from it. Find a pattern everywhere in a file e.g. file.html and print їїї and її.

I would be particularly grateful if you could also explain me how your code works in detail.

What I have done:

findstr "<a href=" file.html

It will find a string with a pattern but I don't know how to put that in a variable and proceed it later to get rid of <a href=", "> and </a>

also I have tried to use for /f "tokens=1,2delims=<a href="" but there is an error with quotation marks. It recognise the " after href= as an opening quotation mark

A option with regex is not a solution. But write to me if you have an idea how that can be done.

1 Answers

Use filter FIND.EXE to print proper lines then parse them all by FOR.

@echo off
setlocal enabledelayedexpansion
for /f "usebackq skip=2 tokens=2* delims== " %%A in (`find/i"<a href=" file.htm`) do (
 set "string=%%B"
 set a=!string:^>=_!
 set b=!a:^<=_!
 for /f "tokens=1,2,* delims=_" %%i in ("!b!") do echo %%~i %%j 
)
endlocal

This code example doesn't check for improper values or split strings. So turn your solutions into the Powershell.

Related