Sumith08
Sumith08

Reputation: 770

how to extract a string between two patterns using batch script?

I have a below line in a pom.xml file.

<abc>xyz</abc>

I need to extract xyz by doing a pattern matching using batch script same like sed -e 's/(.*)/\1/'

and the output should be xyz.

Can anyone please help?

Upvotes: 0

Views: 219

Answers (2)

user7818749
user7818749

Reputation:

You can use <> as delims.

@for /f "tokens=2delims=><  " %%i in ('type pom.xml') dodo set "result=%%i" & goto :show
:show
echo %result%

or if you want a specific value only based on the property name of the tag..

@for /f "tokens=2delims=><  " %%i in ('type pom.xml ^| find /I "abc"') do set "result=%%i" & goto :show
:show
echo %result%

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

you can try with xpath.bat:

call xpath.bat pom.xml "//abc"

to save it to a variable:

for /f "tokens=* delims=" %%# in ('xpath.bat pom.xml "//abc"') set "abc_value=%%#"
echo %abc_value%

Upvotes: 0

Related Questions