Extract data between 2 words.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glenfs
Posts: 6
Joined: 03 Jul 2012 01:28

Extract data between 2 words.

#1 Post by glenfs » 03 Jul 2012 01:38

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.

einstein1969
Expert
Posts: 976
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Extract data between 2 words.

#2 Post by einstein1969 » 03 Jul 2012 03:42

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%"

glenfs
Posts: 6
Joined: 03 Jul 2012 01:28

Re: Extract data between 2 words.

#3 Post by glenfs » 03 Jul 2012 03:54

Hi einstein1969, Thats great..
Thank you very much. That resolved my question. :)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Extract data between 2 words.

#4 Post by Ed Dyreen » 03 Jul 2012 04:07

'
glenfs wrote:i needed to write a program which extracts data from a text.
I'll assume you meant textFile...

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. . .

glenfs
Posts: 6
Joined: 03 Jul 2012 01:28

Re: Extract data between 2 words.

#5 Post by glenfs » 03 Jul 2012 04:16

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?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Extract data between 2 words.

#6 Post by Ed Dyreen » 03 Jul 2012 04:47

'
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. . .
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.

Post Reply