for loop parsing problem in XP

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

for loop parsing problem in XP

#1 Post by taripo » 10 Apr 2015 05:51

I am getting this strange output. (OS is XP as mentioned in title)

I am trying to use this command to grab an IP address that is 10.0.0.5

Primarly to test piping to the FOR command. Hence i'm piping to it and using 'more' in the (..)


But it is not getting the 10 part of the IP.

Any idea what is happening here?

What is going wrong?

Code: Select all

C:\>ipconfig | findstr "Address" | for /f "tokens=13" %f in ('more') do @echo %f

.

C:\>ipconfig | findstr "Address" | for /f "tokens=14" %f in ('more') do @echo %f

:

C:\>ipconfig | findstr "Address" | for /f "tokens=15" %f in ('more') do @echo %f

 0.0.0.5

C:\>ipconfig | findstr "Address" | for /f "tokens=15" %f in ('more') do @echo %f
 >c:\blah\p.p

C:\>type c:\blah\p.p
  .0.0.5


This works

Code: Select all

C:\>for /f "tokens=15" %f in ('ipconfig ^| findstr "Address"') do @echo %f
10.0.0.5

C:\>


But I want to do it via the piping method, hence with 'more' in the round brackets
Last edited by taripo on 10 Apr 2015 08:59, edited 1 time in total.

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

Re: for loop parsing problem in XP

#2 Post by foxidrive » 10 Apr 2015 06:27

It's not a reliable technique by the look of it: this is what I get:

d:\abc>ipconfig | findstr "Address" | for /f "tokens=13" %f in ('more') do @echo %f
:
:

d:\abc>ipconfig | findstr "Address" | for /f "tokens=14" %f in ('more') do @echo %f
192.168.0.10
192.168.56.1

d:\abc>ipconfig | findstr "Address" | for /f "tokens=15" %f in ('more') do @echo %f

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: for loop parsing problem in XP

#3 Post by taripo » 10 Apr 2015 06:41

well, looks like it works for you but not for me

I don't know why yours isn't token=15

In XP it's surely token=15

Code: Select all

C:\>ipconfig | find "Address"
        IP Address. . . . . . . . . . . . : 10.0.0.5

C:\>


first token is IP
second token is Address.
the 13th token is the last dot.
14th token is colon
15th is IP

though mine skips the 1 of 10. for some reason

In Win7 it's the 14th token.
IPv4 Address. . . . . . . . . . . : 192.168.56.1

The For works in Win7. Maybe you tested it in Win7?

My issue is with the For in XP

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

Re: for loop parsing problem in XP

#4 Post by foxidrive » 10 Apr 2015 07:06

I'm sorry Taripo, your question didn't include the OS so I wasn't aware of that.

I can't speak for anyone else but I seldom read subject lines and expect the pertinent details to be in the question. That was my mistake.

This is what I get in Windows XP Pro.

c:\>ipconfig | findstr "Address" | for /f "tokens=13" %f in ('more') do @echo %f
.

c:\>ipconfig | findstr "Address" | for /f "tokens=14" %f in ('more') do @echo %f
:

c:\>ipconfig | findstr "Address" | for /f "tokens=15" %f in ('more') do @echo %f
92.168.0.12


Piping into a for loop has been discussed before and with the limitations discussed.

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: for loop parsing problem in XP

#5 Post by taripo » 10 Apr 2015 09:01

I suppose I could put XP in the body of the question rather than the title. I've just amended the body to mention XP too.

You write that "Piping into a for loop has been discussed before and with the limitations discussed"

It would be useful if you knew Where that was e.g. a link to the thread

It'd be good to know exactly what those limitations are and what is happening there that causes it to eat the number 1

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

Re: for loop parsing problem in XP

#6 Post by Squashman » 10 Apr 2015 12:35

Why not this way?

Code: Select all

ipconfig | findstr "Address" | for /f "tokens=2 delims=:" %f in ('more') do @echo %f


XP

Code: Select all

C:\Documents and Settings\XPMUser>ipconfig | findstr "Address" | for /f "tokens=2 delims=:" %f in ('more') do @echo %f
 192.168.131.65

7

Code: Select all

C:\>ipconfig | findstr "Address" | for /f "tokens=2 delims=:" %f in ('more') do @echo %f
 10.28.16.58

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: for loop parsing problem in XP

#7 Post by Aacini » 10 Apr 2015 14:16

Also, why do you use MORE command? The "natural" way to do that is this:

Code: Select all

for /f "tokens=2 delims=:" %f in ('ipconfig ^| findstr "Address"') do @echo %f

If you don't like to type the "^|" part, you may use this form:

Code: Select all

ipconfig | for /f "tokens=2 delims=:" %f in ('findstr "Address"') do @echo %f

Antonio

Post Reply