Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

for /f command : special delims character

5,580 views
Skip to first unread message

refigh

unread,
Oct 9, 2011, 1:37:37 AM10/9/11
to
Hi,
I wanted to use for /f command to parse a file. one of delimes
character used to separate the words is " character. (plus character
_ ). but for command does not accept it :
for /f "tokens=1-6 delims=_"" %%i in ("%%a") do ( ......
produces error.
how can I assign character " as delims?

Timo Salmi

unread,
Oct 9, 2011, 3:16:34 AM10/9/11
to

One way around it is first to replace the " characters with a more
delims-tolerant character. Somewhat along the lines of e.g.

34} How can I remove the quote characters from a line?
http://www.netikka.net/tsneti/info/tscmd034.htm

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

foxidrive

unread,
Oct 9, 2011, 4:30:42 AM10/9/11
to
On 9/10/2011 18:16, Timo Salmi wrote:
> On 09.10.2011 08:37 refigh wrote:
>> I wanted to use for /f command to parse a file. one of delimes
>> character used to separate the words is " character. (plus character
>> _ ). but for command does not accept it :
>> for /f "tokens=1-6 delims=_"" %%i in ("%%a") do ( ......
>> produces error.
>> how can I assign character " as delims?
>
> One way around it is first to replace the " characters with a more
> delims-tolerant character. Somewhat along the lines of e.g.
>
> 34} How can I remove the quote characters from a line?
> http://www.netikka.net/tsneti/info/tscmd034.htm
>
> All the best, Timo


You can optionally shows us some of the text and explain what you need to parse. There are some ways to get around this, depending on the text layout.


--
Regards,
Mic

refigh

unread,
Oct 9, 2011, 4:49:23 AM10/9/11
to


thanks
I think first solution of your link is good for me, but I have a
problem,
set command does not work in for /f..... do ( ) why?
for example, below line print "test" not "test2"

@echo off
set VERSION_SRC=XilinxPrj\Embed_proc_AXI\pcores\version_v1_00_a\hdl
\vhdl\user_logic.vhd
set line_=test

for /D %%d in ( ???_? ) do (
for /f "tokens=5-12 delims=;_X " %%i in ('findstr /I /R /
c:"hw_version_brch.*<=.*" %%d\%VERSION_SRC%') do (
set line_=test2
echo. %line_%
)
)
@echo on

Petr Laznovsky

unread,
Oct 9, 2011, 5:15:17 AM10/9/11
to
> @echo on- Hide quoted text -
>
> - Show quoted text -

You have to declare:

setlocal ENABLEDELAYEDEXPANSION

and use:

echo. !line_!

instead of

echo. %line_%

L.

refigh

unread,
Oct 9, 2011, 6:37:54 AM10/9/11
to
thank you man
It is solved

jeb

unread,
Oct 9, 2011, 1:15:05 PM10/9/11
to

echo( should be prefered against echo., as it is ~10 times faster and
more robust.

To use a quote character as a delim character in a FOR /F loop is
posible,
it only looks a bit ugly.

setlocal EnableDelayedExpansion
set "var=one"two"three"
FOR /f tokens^=1-3^ delims^=^" %%a in ("!var!") do echo %%a--%%b--%%c

found by pieh-ejdsch last month http://www.administrator.de/index.php?content=172844
(german batch forum)

jeb

billious

unread,
Oct 10, 2011, 1:36:49 AM10/10/11
to
jeb wrote:
>
> echo( should be prefered against echo., as it is ~10 times faster and
> more robust.
>
> To use a quote character as a delim character in a FOR /F loop is
> posible,
> it only looks a bit ugly.
>
> setlocal EnableDelayedExpansion
> set "var=one"two"three"
> FOR /f tokens^=1-3^ delims^=^" %%a in ("!var!") do echo %%a--%%b--%%c
>
> found by pieh-ejdsch last month
> http://www.administrator.de/index.php?content=172844
> (german batch forum)
>
> jeb

FOR /f tokens^=1-3^ delims^=^" %%a in ("!var!") do echo %%a--%%b--%%c

????

I'm not sure whether I'm more astonished that this works or that someone
would attempt to use such "obviously-broken" syntax.

(but the escaped-space after the "3" is redundant. If the superfluous space
exists, it must be escaped however...)

billious

unread,
Oct 10, 2011, 2:01:17 AM10/10/11
to
On further experimentation, I found that to specify a number of separators
for DELIMS

1. standard characters may but need not be escaped by ^
2. ^[space] must be last, if it used
3. = and & and ^ require to be escaped by ^
4. % needs to be escaped by %

(testing incomplete)


jeb

unread,
Oct 10, 2011, 3:23:30 AM10/10/11
to

The space behind the 3 may be superfluous, but normally you add it to
the options like
FOR /F "tokens=1-3 delims=X" looks better than FOR /F
"tokens=1-3delims=X"

The syntax is a bit compliacated as all the standard delims for a
batch line are active there ;,=<space> and <tab>
Obviously the special characters like ^&|<> have to be escaped
Ther percent must always be escaped, even if it is in a `normal`
quoted option string.

I build a similar string for disabling the EOL character (which can't
be empty), by setting it to the line feed character.

for /f ^"tokens^=1-3^ eol^=^

^" %%a in (myFile) do ...

The line break is necessary for inserting the LF.
But even if I used the "escaping" syntax I never got the idea to
remove the quotes completly, and with the surrounding quotes you can't
set the delims to the quote character.

jeb

Timo Salmi

unread,
Oct 10, 2011, 12:11:38 PM10/10/11
to
On 09.10.2011 11:49 refigh <ham...@gmail.com> wrote:
> On Oct 9, 10:16 am, Timo Salmi <t...@uwasa.fi> wrote:
>> 34} How can I remove the quote characters from a line?
>> http://www.netikka.net/tsneti/info/tscmd034.htm

> thanks
> I think first solution of your link is good for me, ...

Good. You might also find the following of some interest
http://www.netikka.net/tsneti/info/tscmd072.htm#vbs

All the best, Timo

--

jeb

unread,
Oct 10, 2011, 4:00:56 PM10/10/11
to
On 10 Okt., 18:11, Timo Salmi <t...@uwasa.fi> wrote:

> On 09.10.2011 11:49 refigh <hamz...@gmail.com> wrote:
>
> > On Oct 9, 10:16 am, Timo Salmi <t...@uwasa.fi> wrote:
> >>  34} How can I remove the quote characters from a line?
> >>  http://www.netikka.net/tsneti/info/tscmd034.htm
> > thanks
> > I think  first solution of your link is good for me, ...
>
> Good. You might also find the following of some interest
>  http://www.netikka.net/tsneti/info/tscmd072.htm#vbs
>
>     All the best, Timo
>
> --
> Prof. (emer.) Timo Salmi, Vaasa, Finlandhttp://www.netikka.net/tsneti/homepage.php
> Useful CMD script trickshttp://www.netikka.net/tsneti/info/tscmd.php

Yes, but now it's outdated.
With using directly the quotes as delims the workaround is no longer
necessary

jeb

frank.w...@gmail.com

unread,
Oct 10, 2011, 1:03:51 PM10/10/11
to
^ To use a quote character as a delim character in a ^ FOR /F loop is posible, it only looks a bit ugly. ^ setlocal EnableDelayedExpansion ^ set "var=one"two"three" ^ FOR /f tokens^=1-3^ delims^=^" %%a in ("!var!") ^ do echo %%a--%%b--%%c ^ found by pieh-ejdsch last month It seems that the quotes enclosing the options string are only necessary to prevent the command line delimiters from being recognized and thus causing the line to be separated into useless statements. In most cases an alternative to quotes is to escape the command line delimiters. Since CMD is also looking for quotes to handle, the quote as a 'delim' must also be escaped. Delimiters are expected where I've placed commas on the last statement below. The last statement also adds 'r' as a 'delim'. @Echo OFF SetLocal EnableExtensions EnableDelayedExpansion Set "var=one"two"three" Set var Echo( & Echo(delims^=^"^ tokens^=1-3 FOR /F delims^=^"^ tokens^=1-3 %%a in ("%var%") Do Echo 1=[%%a] 2=[%%b] 3=[%%c] FOR /F delims^=^"^ tokens^=1-3 %%a in ("!var!") Do Echo 1=[%%a] 2=[%%b] 3=[%%c] FOR /F delims^=^"^ tokens^=1-3 %%a in ("one"two"three") Do Echo 1=[%%a] 2=[%%b] 3=[%%c] Echo( & Echo(usebackq^ delims^=^"^ tokens^=1-3 FOR /F usebackq^ delims^=^"^ tokens^=1-3 %%a in ('%var%') Do Echo 1=[%%a] 2=[%%b] 3=[%%c] FOR /F usebackq^ delims^=^"^ tokens^=1-3 %%a in ('!var!') Do Echo 1=[%%a] 2=[%%b] 3=[%%c] FOR /F usebackq^ delims^=^"^ tokens^=1-3 %%a in ('one"two"three') Do Echo 1=[%%a] 2=[%%b] 3=[%%c] Echo( & Echo(delims^=^"r^ tokens^=1-4 FOR,/F,delims^=^"r^ tokens^=1-4,%%a,in,("%var%"),Do,Echo,1=[%%a] 2=[%%b] 3=[%%c] 4=[%%d] Frank Please pardon my UTF-8 testing while creating my news reader. U+20A0 U+2160

jeb

unread,
Oct 11, 2011, 7:12:28 AM10/11/11
to
On 10 Okt., 19:03, frank.westl...@gmail.com wrote:
> ^ To use a quote character as a delim character in a
> ^ FOR /F loop is posible, it only looks a bit ugly.
> ^
> ^ setlocal EnableDelayedExpansion
> ^ set "var=one"two"three"
> ^ FOR /f tokens^=1-3^ delims^=^" %%a in ("!var!")
> ^ do echo %%a--%%b--%%c
> ^
> ^ found by pieh-ejdsch last month
>
> It seems that the quotes enclosing the options string are only
> necessary to prevent the command line delimiters from being
> recognized and thus causing the line to be separated into useless
> statements. In most cases an alternative to quotes is to escape
> the command line delimiters. Since CMD is also looking for quotes
> to handle, the quote as a 'delim' must also be escaped. Delimiters
> are expected where I've placed commas on the last statement below.
> The last statement also adds 'r' as a 'delim'.

Yes, a time ago I tested it with FOR /L, the following lines are doing
excactly the same,
as only the delims are necessary while parsing, the rest will be
ignored

FOR /L %%n in (1,1,10) DO echo %%n
FOR /L %%n in (1 1 10) DO echo %%n
FOR /L %%n in (1;1;10) DO echo %%n
FOR /L %%n in (1=1=10) DO echo %%n
FOR /L %%n in (1;,,;==,,; 1;=,,;;==;,;;10) DO echo %%n
FOR /L %%n in (1+Pi/2=1-sin(360°^)=10E-20*array[4]) DO echo %%n

jeb

Timo Salmi

unread,
Oct 12, 2011, 8:44:30 AM10/12/11
to
On 10.10.2011 23:00 jeb <xi...@gmx.de> wrote:
> On 10 Okt., 18:11, Timo Salmi <t...@uwasa.fi> wrote:
>> Good. You might also find the following of some interest
>> http://www.netikka.net/tsneti/info/tscmd072.htm#vbs

> Yes, but now it's outdated.

I have to disagree with such a categoric assessment. Esoteric,
undocumented tweaks do not outdate the more regular solutions in the
true sense of the word.

> With using directly the quotes as delims the workaround is no longer
> necessary

But they can be useful and certainly are highly interesting, even
intriguing.

All the best, Timo

--
Message has been deleted
0 new messages