How to get string from each line between specified characters?

时间:2016-10-20 12:59:10

标签: batch-file substring

I have a text file, which has similar lines, like:

what to do: do something; when to do: now
what to do: drink; when to do: always
what to do: eat; when to do: sometimes
what to do: laugh a lot; when to do: always

I want a batch script, where I can get all the "what to do" strings from each line, so the output would be like:

do something
drink
eat
laugh a lot

So these words are coming from between "what to do: " and "; when to do" string in each line. (Or just between "what to do: " and ";")

Many thanks!

2 个答案:

答案 0 :(得分:1)

Try this :

@echo off
 for /f "tokens=2 delims=:;" %%a in (YourFile.txt) do echo %%a

If you also need the last element (now,always,sometimes)

@echo off
 for /f "tokens=2,4 delims=:;" %%a in (log.txt) do echo %%a -^> %%b

答案 1 :(得分:0)

一个选项:

@Echo Off
Set "IFN=myfile.txt"
Set "TTF=what to do: "
For /F "Delims=" %%A In ('Find "%TTF%"^<%IFN%') Do Call :Sub %%A
Timeout -1 1>Nul
Exit/B

    :Sub
    Set _=%*
    Call Set SFL=%%_:*%TTF%=%%
    Set SFL=%SFL:; =&:%
    Echo=%SFL%

仅在第二行和第三行进行适当的更改,在这种情况下请注意第三行字符串末尾的重要空格。