Home > Blockchain >  How to split a string from the for loop to get the beginning and the rest of it?
How to split a string from the for loop to get the beginning and the rest of it?

Time:05-12

I have this file:

v1 test
v2 test test test
v3 test test
v4 test test test test test test
v5 test

How to split the first word from the rest in a for loop? I need this result:

B: v1
E: test

B: v2
E: test test test

B: v3
E: test test

B: v4
E: test test test test test test

B: v5
E: test

I'm trying to use a code like this:

for /f "tokens=*" %%s in ('type file.txt') do (
    echo %%s
    echo %%s
    echo.
)

CodePudding user response:

You were very close, but you need to specify a second token in order to use it. Thankfully, using tokens=1,* does exactly what you want, setting %%s to the first token and %%t to the rest of the line.

for /f "tokens=1,*" %%s in ('type file.txt') do (
    echo B: %%s
    echo E: %%t
    echo.
)
  • Related