victorio
victorio

Reputation: 6646

How to get string from each line between specified characters?

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!

Upvotes: 0

Views: 48

Answers (2)

Compo
Compo

Reputation: 38589

One option:

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

Make your appropriate changes only on lines two and three, noting in this case the important space at the end of the string on line three.

Upvotes: 0

SachaDee
SachaDee

Reputation: 9545

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

Upvotes: 1

Related Questions