Extract specific text withing a file using DOS batch script

October 15, 2012 at 13:40:52
Specs: Windows XP
Hey all,

I have a protocol-kind-of file which contains some entries like these:

Entry 1 =
(Some text
(More text)
)

Entry 2 =
(Some text
(More text)
(More text)
(More text)
)

Entry 3 =
(Some text
(More text)
(More text)
)

I want to extract Entry 2 data. I have tried for a week to find a solution I already have the way of moving entry 1 to a temp file but no luck when working with parenthesis lines. As you may see there's one open parenthesis then other parenthesis pairs and finally a closing parenthesis. I was thinking that I can have one var for open and other one for the closing ones and subtract until the result is zero. This have to be done merely using Windows batch programming due to server restrictions.

Thanks in advance.

Jesus


See More: Extract specific text withing a file using DOS batch script

Report •


#1
October 15, 2012 at 16:33:57
Are there actually CRLF's between each entry?

Tony


Report •

#2
October 15, 2012 at 17:40:30
this seemed to work on simple test data:

::===== begin script
@echo off & setlocal
set two=
for /f "tokens=1,2 delims=()" %%a in (tst.par) do (
if /i "%%a" equ "entry 2 =" set two=two
if /i "%%a" equ "Entry 3 =" goto :eof
if defined two echo [%%a]
)
::==== end script

assuming crlf between () elements, as Tony was asking about.


Report •

#3
October 16, 2012 at 07:35:16
Yes there are always CRLF after each entry even the last one. BTW what I need is to get rid of the specific paragraph rather than extract it (My mistake when redacting) I need text for Entry 1 and Entry 3 to be echoed to another file. Each entry is a name of a server connection and i want to modified more than 1 file thus Entries are not always name the same. Thanks!

Report •

Related Solutions

#4
October 16, 2012 at 07:38:44
Thanks for your script I tried but it's giving me the following:
[Entry 1 =]
[ ]
[ ]
[ ]

BTW what I need is to get rid of the specific paragraph rather than extract it (My mistake when redacting) I need text for Entry 1 and Entry 3 to be echoed to another file. Each entry is a name of a server connection and i want to modified more than 1 file thus Entries are not always name the same. Thanks!


Report •

#5
October 16, 2012 at 17:54:15
well, try this one. The other one worked on my computer, for what you first described, so now I doubt if anything I write will ever work again.

::====== begin ENTRY.BAT
@echo off>build.txt & setlocal
if "%1" equ "" echo argument required. Usage: ENTRY nn
for /f "tokens=1 delims=[]" %%a in ('find /i /n "entry %1 ="^<tst.par') do set L1=%%a
for /f "tokens=1 delims=:" %%a in ('findstr /n /b /e ")" tst.par') do (
if %%a gtr %L1% set L2=%%a & goto :xx
)
:xx
rem debug echo %L1% %L2%
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<tst.par') do (
if %%a equ %L1% goto :yy
>> build.txt echo.%%b
)
:yy
for /f "skip=%L2% tokens=*" %%a in (tst.par) do >> build.txt echo %%a
::====== end script

then i noticed "entries are server connections", so after this, I give up - I'm clueless in Kansas and waiting for the Scarecrow (the only one with a "brane").


Report •

Ask Question