splitting a variable when there are special characters
Moderator: DosItHelp
splitting a variable when there are special characters
set var=123456
Set newvar=%var:~2,4%
echo %newvar%
should give me 3456 (i think)
Here is my problem, i have a string of about 100 digits where the first 46 are not needed, however it contains <>. When i do the above to split it up a bit, it doesnt work.
Thu Mar 28 00:00:51 2013: <456>Mar 28 00:02:43 ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS
in shell scripts i can use "" to encapsulate the data and ensure it doesnt cause any issues, but in dos, the <> appears to cause it to crash.. any way i can work around this?
Set newvar=%var:~2,4%
echo %newvar%
should give me 3456 (i think)
Here is my problem, i have a string of about 100 digits where the first 46 are not needed, however it contains <>. When i do the above to split it up a bit, it doesnt work.
Thu Mar 28 00:00:51 2013: <456>Mar 28 00:02:43 ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS
in shell scripts i can use "" to encapsulate the data and ensure it doesnt cause any issues, but in dos, the <> appears to cause it to crash.. any way i can work around this?
Re: splitting a variable when there are special characters
Looks like delimited fields. Is your data always the 6th field?
Re: splitting a variable when there are special characters
Code: Select all
@echo off
echo From A file
for /F "tokens=6 delims=|" %%G in (file.txt) do echo %%G
echo From A Variable
set "myvar=Thu Mar 28 00:00:51 2013: <456>Mar 28 00:02:43 ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
for /F "tokens=6 delims=|" %%G in ("%myvar%") do echo %%G
pause
Output
Code: Select all
From A file
ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS
From A Variable
ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS
Press any key to continue . . .
Re: splitting a variable when there are special characters
Brilliant, however i missed something - 6th field onwards... there are about 9 or 10 fields..
Re: splitting a variable when there are special characters
Use TOKENS=5* and then use echo %%H. The 5th field will be in %%G so you need to use %%H to get the 6th field and beyond.
Re: splitting a variable when there are special characters
chr15b wrote:set var=123456
Set newvar=%var:~2,4%
echo %newvar%
should give me 3456 (i think)
Here is my problem, i have a string of about 100 digits where the first 46 are not needed, however it contains <>. When i do the above to split it up a bit, it doesnt work.
This will work to set the variable.
set "var=Thu Mar 28 00:00:51 2013: <456>Mar 28 00:02:43 ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
Set "newvar=%var:~46%"
echo "%newvar%"
Re: splitting a variable when there are special characters
foxidrive wrote:chr15b wrote:set var=123456
Set newvar=%var:~2,4%
echo %newvar%
should give me 3456 (i think)
Here is my problem, i have a string of about 100 digits where the first 46 are not needed, however it contains <>. When i do the above to split it up a bit, it doesnt work.
This will work to set the variable.
set "var=Thu Mar 28 00:00:51 2013: <456>Mar 28 00:02:43 ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
Set "newvar=%var:~46%"
echo "%newvar%"
Sadly this doesn't, i think its some issue with the dos window is called from another dos window, but in your example, %var% is blank
Re: splitting a variable when there are special characters
It most certainly does work. Here is the output. Copy and paste the code for yourself.
" ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
If it does not work in your situation then you haven't supplied real life data to see where it fails.
" ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
If it does not work in your situation then you haven't supplied real life data to see where it fails.
Re: splitting a variable when there are special characters
foxidrive wrote:It most certainly does work. Here is the output. Copy and paste the code for yourself.
" ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
If it does not work in your situation then you haven't supplied real life data to see where it fails.
It does not work in conjunction with the above scenario.
using the for command to get %%G to equal ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS
echo %%G gives correct output.
set myvar=%%G - %myvar% is blank
Code: Select all
for /F "tokens=6 delims=|" %%G in (file.txt) do (
echo %%G
set myvar1=%%G
set "myvar2=%%G"
echo %myvar1%
echo %myvar2%
)
both the last echos are blank.
Re: splitting a variable when there are special characters
foxidrive wrote:It most certainly does work. Here is the output. Copy and paste the code for yourself.
" ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS"
If it does not work in your situation then you haven't supplied real life data to see where it fails.
Sorry, also to add, the string is actually "Thu Mar 28 00:00:51 2013: <456>Mar 28 00:02:43 ABC:0|Output inc.|SOFTWARE VER|v1.1|123|ALLOFMYDATAISFROMTHISPOINTONWARDSANDDOESNOTCONTAINANYSPECIALCHARS" which may be why your example works, yours does not contain the <> symbols that i believe are the cause of the problem.
Re: splitting a variable when there are special characters
A) You have no idea about delayed expansion, and *that* is the reason the variables will not display. Alternately you can display them outside the loop.
B) My string does indeed contain <and> if you took the time to check.
B) My string does indeed contain <and> if you took the time to check.
Re: splitting a variable when there are special characters
foxidrive wrote:A) You have no idea about delayed expansion, and *that* is the reason the variables will not display. Alternately you can display them outside the loop.
B) My string does indeed contain <and> if you took the time to check.
I concur with Foxidrive.
His code will work.
The additions you made to my code do not work because you are not using Delayed Expansion.