Page 1 of 1

Is there a way to split a string to new lines in file by delimiter

Posted: 11 Jun 2020 00:26
by brentonv
In batch I'm trying to split a string into substrings preferably based on a delimiter.

This example works but is there a better way to do this:

Code: Select all

for /f "tokens=1-20 delims=:" %%a in (string.txt) do (
echo %%a& echo.%%b& echo.%%c& echo.%%d& echo.%%e& echo.%%f& echo.%%g& echo.%%h& echo.%%i& echo.%%j
echo.%%k& echo.%%l& echo.%%m& echo.%%n& echo.%%o& echo.%%p& echo.%%q& echo.%%r& echo.%%s& echo.%%t
) >substrings.txt
I have tried https://stackoverflow.com/a/40384571/13515739 from Magoo which works for default delimiters but fails using colon :

Code: Select all

@echo off
setLocal 
for /f "delims=:" %%a in (string.txt) do (
for %%i in (%%a) do echo %%i >>substrings.txt
)
I have also tried viewtopic.php?t=6429#p41035 from Aacini but it seems to fail using special characters.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set i=1
set "x=%string%"
set "x!i!=%x::=" & set /A i+=1 & set "x!i!=%"
set x
An example %string% var or string.txt is:

Code: Select all

Select project:/option:report name="Sustainability":option value="201":Huonville:/option:/report:report name="Energy and Water Management":option value="102":Cygnet:/option:/report:report name="Tree Management":option value="101":Cygnet:/option:/report:option disabled="disabled":/option:/select
I am trying to output %string% var or string.txt to substring.txt as follows:

Code: Select all

Select project
/option
report name="Sustainability"
option value="201"
Huonville
/option
/report
report name="Energy and Water Management"
option value="102"
Cygnet
/option
/report
report name="Tree Management"
option value="101"
Cygnet
/option
/report
option disabled="disabled"
/option
/select
Any help would be greatly appreciated. Thanks

Re: Is there a way to split a string to new lines in file by delimiter

Posted: 11 Jun 2020 05:27
by jeb
Hi brentonv,

you link to SO Split values into separate lines for each delimiter - batch shows already a working solution :D

Code: Select all

@echo off
setLocal EnableDelayedExpansion
for /f "delims=" %%a in (string.txt) do (
    set "line=%%a"
    echo reading line: !line!
    for /F "delims=" %%C in (""!line:^:^=^"^
"!"^") do (
        set "col=%%~C"
        echo   - !col!
    )
)
This uses the trick to replace the delimiter with a new line character.
The surrounding quotes are only for stability against problematic start characters

Re: Is there a way to split a string to new lines in file by delimiter

Posted: 11 Jun 2020 09:40
by brentonv
Thanks Jeb, I'll try it tomorrow and let you know how it goes :D

Re: Is there a way to split a string to new lines in file by delimiter

Posted: 12 Jun 2020 22:23
by brentonv
Actually I'm pretty sure I tried it when I first came across that SO post. I tested it again and using my example it only pulls the first token. Thanks anyway.

Re: Is there a way to split a string to new lines in file by delimiter

Posted: 13 Jun 2020 06:06
by brentonv
Stephan from SO worked out this code for me and it handles all special characters https://stackoverflow.com/a/62320627/13515739
Posting here for anyone else looking for a solution to this.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set /p string=<"input.txt"
set "string=%string: =@%"
set "string="%string::=" "%"
(for %%a in (%string%) do (
  set "output=%%~a"  
  echo !output:@= !
))>output.txt
Set string or input text file (modify line 3).

Code: Select all

set /p string=<"input.txt"
The delimiter in this example is colon [:] which follows after [string:] (modify line 5).

Code: Select all

set "string="%string::=" "%"
If string or input.txt contains quotes %string% may need to be double quoted (modify line 3).

Code: Select all

for /f "delims=" %%a in (input.txt) do (set string="%%a")

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (input.txt) do (set string="%%a")
set "string=%string: =@%"
set "string="%string::=" "%"
(for %%a in (%string%) do (
  set "output=%%~a"  
  echo !output:@= !
))>output.txt