简体   繁体   中英

How to search a string and extract another string pattern from a text file using windows batch script

Consider out.txt file has the following format:

Display Variable;

    ID    NAME       value
===*==========*================
201  Variavle     07Dec2014

Display Variable12;

ID    NAME          value
===*==========*================
201  Variavle12     08Dec2014

Display Variable123;

ID    NAME       value
===*==========*================
201  Variavle123     09Dec2014    

In batch file we need to search for Variable , Variable12 and Variable123 and return each string in column value to batch file to environment variables with the names from column NAME like

SET Variavle=09Dec2014
SET Variavle12=08Dec2014
SET Variavle123=09Dec2014

Please help me to get it done.

@echo off

for /f "tokens=2,3 delims= " %%A in ('FINDSTR /i /r  /c:"Variavle.* " out.txt') do (
    set "%%A=%%B"
)

set Variavle

Following code helped me to find the answer

SET Variavle=09Dec2014

SET Variavle12=08Dec2014 SET Variavle123=09Dec2014

@echo off   
for /f "tokens=* " %%A in ('FINDSTR /i /r  /c:"Variable     " D:\test.txt') do (    
SET _test=%%A
SET Variable=%_test:~20,5%
ECHO %Variable%
)    

... ...

Let me know if any changes !

Thanx for the help.

@npocmaka

"dev.bat"

@echo off
for /f "tokens=* " %%A in ('FINDSTR /i /r /c:"nBegin_fcst_dt " D:\\test.txt') do (
SET _test=Variavle 20142 SET nBegin_fcst_dt=%_test:~13,5% ECHO:%nBegin_fcst_dt% ) "D:\\test.txt" ID NAME value === ========== ================ 201 Variavle 20142

I just ran the above script like "test.bat"

I got some weird output.

Expected output 20142

when i ran the script, i got the actual output at third time

First time: Nothing

Second time:~13,5 Third time:20142

D:>dev.bat

D:>dev.bat ~13,5 D:>dev.bat 20142

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM