1.

Solve : using " as delim?

Answer»

I want to make simple converter and what i need to do:

input file containing line:
Code: [Select]<Hotspot X="123.456" Y="31.456" Z="2.456" />output file:
Code: [Select]123.456 31.456 2.456
The way i thought would need to use " as a delim:
Code: [Select]@echo off
echo start of file > file2.xml
findstr "X=" file1.xml > tmp
for /F "tokens=2,4,6 delims=" %%G in (tmp) do ( echo %%G %%H %%I >> file2.xml)
del tmp
Is it even possible, if not how else could i do that?here's a one liner, using gawk for windows


Code: [Select]C:\test>gawk "/Hotspot/{for(i=2;i<NF;i++){ gsub(/.*=|\042/,\"\",$i);printf $i\" \"}} " file
123.456 31.456 2.456
you could replace quotes in each line that you read in with some other character that will never appear in the input text such as a dollar sign or semicolon

test.txt

Code: [Select]<Hotspot X="123.456" Y="31.456" Z="2.456" />
<Hotspot X="234.567" Y="51.952" Z="8.853" />
<Hotspot X="666.238" Y="93.532" Z="6.991" />

batch script

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in (test.txt) do (
set "string=%%A"
echo input !string!
set string=!string:"=$!
for /f "tokens=1-7 delims=$" %%B in ("!string!") do (
echo output %%C %%E %%G
)
)

output

Code: [Select]input <Hotspot X="123.456" Y="31.456" Z="2.456" />
output 123.456 31.456 2.456
input <Hotspot X="234.567" Y="51.952" Z="8.853" />
output 234.567 51.952 8.853
input <Hotspot X="666.238" Y="93.532" Z="6.991" />
output 666.238 93.532 6.991One more THING if coming to replacing characters, would there be way to replace whole string of characters into single CHAR (for using few types of delimiters)?

Like i would have whole "@ "X=$"-># ... and so on.
Code: [Select]set string=!string:<Hotspot:@!
set string=!string:X=$:#!Would it be like that?

PS: i need raw windows batch, without any pluginsQuote from: drag0nius on January 14, 2011, 11:19:42 PM

One more thing if coming to replacing characters, would there be way to replace whole string of characters into single char (for using few types of delimiters)?

Like i would have whole "<Hotspot"->@ "X=$"-># ... and so on.
Code: [Select]set string=!string:<Hotspot:@!
set string=!string:X=$:#!Would it be like that?

PS: i need raw windows batch, without any plugins

The syntax for string replacement is:

Inside a loop where you are using delayed expansion (and hence using exclamation marks for variables)

Code: [Select]set string=!string:A=B!
Or elsewhere using percent signs

Code: [Select]set string=%string:A=B%
A is any sequence of one or more characters that you wish to search for in %string% and B is zero or more characters that you wish to put in A's place.

Thus if %string1% is:

I like cats

set string2=%string1:cat=dog% would result in %string2% now containing

I like dogs

Notice that I used a NEW string (string2) in the SET statement. If you use the same variable name (e.g. set string=%string:hot=cold%) the string in the variable is modified but if you use a new variable (e.g. set newstring=%oldstring:man=woman%) then a new string is formed reflecting the change and the old string is preserved.

If the replacement consists of a zero length string (i.e. nothing) then the search term is deleted so that if you did this

Code: [Select]set string1=Mary had a little lamb
set string2=%string1:little =%
then you are searching string1 for little followed by one space and replacing it with nothing (there is nothing between the equals sign and the right-hand variable DELIMITER)

and string 2 is therefore:

Mary had a lamb








Quote from: me, above
A is any sequence of one or more characters that you wish to search for in %string% and B is zero or more characters that you wish to put in A's place.

Note that if either sequence contains one or more of certain special control characters e.g. < > & % ! (there are more) then you have to take certain PRECAUTIONS to avoid the script barfing (crashing) at that point.



Discussion

No Comment Found