Hi All,
I am new to Batch programming, i needed to write a program which extracts data from a text. The text would contain words select and From and i need to extract text between the words Select and From and assign it to a variable.
Could you please help?
Regards,
Darryl.
Extract data between 2 words.
Moderator: DosItHelp
-
- Expert
- Posts: 976
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
Re: Extract data between 2 words.
Code: Select all
set str="and select my,your,his(),* from pippo;"
set begin=%str:*select=%
set end=%str:*from=%
call set middle=%%begin:from%end%=%%
echo "%middle%"
Re: Extract data between 2 words.
Hi einstein1969, Thats great..
Thank you very much. That resolved my question.
Thank you very much. That resolved my question.

Re: Extract data between 2 words.
'
This method allows for special characters to appear in text.
I'll assume you meant textFile...glenfs wrote:i needed to write a program which extracts data from a text.
This method allows for special characters to appear in text.
Code: Select all
@echo off &setlocal enableDelayedExpansion &echo.
set "$file=tst.TXT"
set "$pre=select"
set "$pst=from"
echo.$file: '!$file!'
type "!$file!"
echo. &echo. &for /f "usebackq delims=" %%? in (
"!$file!"
) do set "$= %%?" &set "$=!$:*%$pre%=!" &if defined $ if "!$!" neq " %%?" (
set "$w=!$!###"
set "$=!$w:*%$pst%=!"
for %%? in ( "!$!" ) do set "$w=!$w:%%~?=!"
set "$w=!$w:%$pst%=!"
echo.$w: '!$w!'
)
pause
exit
Code: Select all
$file: 'tst.TXT'
Hi All,
I am new to Batch programming, i needed to write a program which extracts data f
rom a text.
The text would contain words select and From and i need to extract text between
the words Select and From and assign it to a variable.
Could you please help?
Regards,
Darryl.
$w: ' and '
Druk op een toets om door te gaan. . .
Re: Extract data between 2 words.
Thank you Ed, 
I had another question, from your example if i have multiple *.txt files(Input files) and i need to do the same (extract data between SELECT and FROM ) from the different file and push the data to different output files , could we be able to do that?

I had another question, from your example if i have multiple *.txt files(Input files) and i need to do the same (extract data between SELECT and FROM ) from the different file and push the data to different output files , could we be able to do that?
Re: Extract data between 2 words.
'
This collects all the words from different input files into one output file.
Did you notice how I use stream 2 ( stderr ) to avoid writing to the file but instead to console. You can use streams to provide different files with data, I didn't need to write >&1 "!$oFile!" because stream 1 is the default stream and it even generates an error if you did, but any other stream will work.
This collects all the words from different input files into one output file.
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$iFile=in.TXT"
set "$oFile=ou.TXT"
set "$pre=select"
set "$pst=from"
> "!$oFile!" (
set /a c = 0 &for %%? in (
"!$iFile!", "!$iFile!", "!$iFile!"
) do (
>&2 (
echo.
echo.$file: '%%~?'
type "%%~?"
echo.
echo.
)
for /f "usebackq delims=" %%? in (
"%%~?"
) do set "$= %%?" &set "$=!$:*%$pre%=!" &if defined $ if "!$!" neq " %%?" (
set /a c += 1
set "w=!$!###"
set "$=!w:*%$pst%=!"
for %%? in ( "!$!" ) do set "w=!w:%%~?=!"
set "w=!w:%$pst%=!"
set "w%c%=!w!"
echo.w!c!: '!w%c%!'
echo.w!c!: '!w%c%!'>&2
)
)
)
echo.
echo.$oFile: '!$oFile!'
type "!$oFile!"
pause
exit
Code: Select all
$file: 'in.TXT'
Hi All,
I am new to Batch programming, i needed to write a program which extracts data f
rom a text.
The text would contain words select and From and i need to extract text between
the words Select and From and assign it to a variable.
Could you please help?
Regards,
Darryl.
w1: ' and '
$file: 'in.TXT'
Hi All,
I am new to Batch programming, i needed to write a program which extracts data f
rom a text.
The text would contain words select and From and i need to extract text between
the words Select and From and assign it to a variable.
Could you please help?
Regards,
Darryl.
w2: ' and '
$file: 'in.TXT'
Hi All,
I am new to Batch programming, i needed to write a program which extracts data f
rom a text.
The text would contain words select and From and i need to extract text between
the words Select and From and assign it to a variable.
Could you please help?
Regards,
Darryl.
w3: ' and '
$oFile: 'ou.TXT'
w1: ' and '
w2: ' and '
w3: ' and '
Druk op een toets om door te gaan. . .