splitting a variable when there are special characters

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

splitting a variable when there are special characters

#1 Post by chr15b » 28 Mar 2013 09:37

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?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: splitting a variable when there are special characters

#2 Post by Squashman » 28 Mar 2013 10:39

Looks like delimited fields. Is your data always the 6th field?

chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Re: splitting a variable when there are special characters

#3 Post by chr15b » 28 Mar 2013 10:48

yes...

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: splitting a variable when there are special characters

#4 Post by Squashman » 28 Mar 2013 10:48

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 . . .

chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Re: splitting a variable when there are special characters

#5 Post by chr15b » 28 Mar 2013 11:02

Brilliant, however i missed something - 6th field onwards... there are about 9 or 10 fields..

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: splitting a variable when there are special characters

#6 Post by Squashman » 28 Mar 2013 11:11

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: splitting a variable when there are special characters

#7 Post by foxidrive » 28 Mar 2013 13:53

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

chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Re: splitting a variable when there are special characters

#8 Post by chr15b » 02 Apr 2013 03:10

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: splitting a variable when there are special characters

#9 Post by foxidrive » 02 Apr 2013 03:45

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.

chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Re: splitting a variable when there are special characters

#10 Post by chr15b » 02 Apr 2013 04:10

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.

chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Re: splitting a variable when there are special characters

#11 Post by chr15b » 02 Apr 2013 04:31

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: splitting a variable when there are special characters

#12 Post by foxidrive » 02 Apr 2013 05:41

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.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: splitting a variable when there are special characters

#13 Post by Squashman » 02 Apr 2013 06:19

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.

Post Reply