07-23-2023, 10:02 AM
I have a text file that is one long string like this:
ISA*00*GARBAGE~ST*TEST*TEST~CLP*TEST~ST*TEST*TEST~CLP*TEST~ST*TEST*TEST~CLP*TEST~GE*GARBAGE*~
And I need it to look like this:
~ST*TEST*TEST~CLP*TEST
~ST*TEST*TEST~CLP*TEST
~ST*TEST*TEST~CLP*TEST
I first tried to add a line at every `~ST` to split the string up, but I can't for the life of me make this happen. I have tried various scripts, but I thought a find/replace script would work best.
@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=~ST
set REPLACETEXT=~ST
for /f "tokens=1,* delims=~" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
Found here
But I'm stuck because (1) the special character `~` makes the code not work at all. It gives me this result:
string:~ST=~ST
The code does nothing at all if using quotes around `"~ST"`. And (2) I can't figure out how to add a line break before `~ST`.
The final task for this would be to delete the `ISA*00*blahblahblah` and `~GE*blahblahblah` lines after all splits have been performed. But I am stuck on the splitting at `~ST` part.
Any suggestions?
ISA*00*GARBAGE~ST*TEST*TEST~CLP*TEST~ST*TEST*TEST~CLP*TEST~ST*TEST*TEST~CLP*TEST~GE*GARBAGE*~
And I need it to look like this:
~ST*TEST*TEST~CLP*TEST
~ST*TEST*TEST~CLP*TEST
~ST*TEST*TEST~CLP*TEST
I first tried to add a line at every `~ST` to split the string up, but I can't for the life of me make this happen. I have tried various scripts, but I thought a find/replace script would work best.
@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=~ST
set REPLACETEXT=~ST
for /f "tokens=1,* delims=~" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
Found here
[To see links please register here]
But I'm stuck because (1) the special character `~` makes the code not work at all. It gives me this result:
string:~ST=~ST
The code does nothing at all if using quotes around `"~ST"`. And (2) I can't figure out how to add a line break before `~ST`.
The final task for this would be to delete the `ISA*00*blahblahblah` and `~GE*blahblahblah` lines after all splits have been performed. But I am stuck on the splitting at `~ST` part.
Any suggestions?