Computing.Net > Forums > Programming > Nested FOR Loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Nested FOR Loop

Reply to Message Icon

Name: keridbey
Date: October 3, 2008 at 14:38:34 Pacific
OS: WinXP
CPU/Ram: 1.8 Ghz / 1 Gb
Product: Dell
Comment:

I'm attempting to parse a comma-delimited text file in order to pick out three sections. Here's a sample of the text:

ab00000,123.12.1.01,IP 1
,123.12.1.02,IP 2
,123.12.1.03,IP 3
,123.12.1.04,IP 4
,123.12.1.05,IP 5
,123.12.1.06,IP 6
,123.12.1.07,IP 7
,123.12.1.08,IP 8
,123.12.1.09,My Last IP

ab00001,123.12.2.01
,123.12.2.02,IP 2
,123.12.2.03,IP 3
,123.12.2.04,IP 4
,123.12.2.05,IP 5
,123.12.2.06,IP 6
,123.12.2.07,IP 7
,123.12.2.08,IP 8
,123.12.2.09,My Last IP

......and so on with the next one.

I need to put the "abXXXXX", the IP address right next to that, and then the IP address on the line that says "My Last IP" together in a text file by themselves in this format:

ab00000,123.12.1.01,123.12.1.09

The master list this is pulling from is quite long, so manually moving the data will take far too long. I'm more experienced in batch file programming, but VBS would be fine as well. I'm unable to use any other programming languages, unfortunately (not my personal system). I know this could be accomplished using nested "FOR /F" commands in a batch file, but I'm having problems trying to get the IP from the "My Last IP" line to match up to the first two variables. Here's essentially what I have:

for /f "tokens=1,2 delims=," %%a in ('type "MyCSVFile.csv" ^| findstr "st0"') do ('for /f "tokens=2 delims=," %%c in ('type "MyCSVFile.csv" ^| findstr

"Router / Gateway"') & (echo %%a,%%b,%%c>>MyOutput.txt)

It doesn't seem to like the second "FOR" command. Any help would be greatly appreciated!



Sponsored Link
Ads by Google

Response Number 1
Name: Judago
Date: October 3, 2008 at 15:02:27 Pacific
Reply:

I think you need to add and extra ) at the end so you close both for loops and not just one of them.


0

Response Number 2
Name: Judago
Date: October 3, 2008 at 16:40:47 Pacific
Reply:

Provided your list follows the same structure the below script should do the job. Be aware it is a little on the slow side because of the findstr commands.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1,2 DELIMS=," %%G IN (YOURTEXTFILE.TXT) DO (
CMD /C "ECHO %%G"^|FINDSTR /I /R AB[0-9][0-9][0-9][0-9][0-9]>NUL
IF !ERRORLEVEL!==0 SET LIST=%%G,%%H&&SET SW=1
CMD /C "ECHO %%H"^|FINDSTR /I LAST>NUL
IF !ERRORLEVEL!==0 (ECHO !LIST!,%%G,%%H>>NEWFILE.TXT) ELSE IF !SW!==0 SET LIST=!LIST!,%%G
SET SW=0
)


I wasn't sure if you wanted the "my last ip" text included at the end of each line so I included it. If you don't want it change this line:


IF !ERRORLEVEL!==0 (ECHO !LIST!,%%G,%%H>>NEWFILE.TXT) ELSE IF !SW!==0 SET LIST=!LIST!,%%G

To this line:

IF !ERRORLEVEL!==0 (ECHO !LIST!,%%G>>NEWFILE.TXT) ELSE IF !SW!==0 SET LIST=!LIST!,%%G


0

Response Number 3
Name: keridbey
Date: October 7, 2008 at 12:44:47 Pacific
Reply:

Thanks for your help, Judago! Your script works quite well. Would it be possible to omit the info in the middle, or would that have to be separately parsed?

This stuff removed from the output:
,123.12.2.02,IP 2
,123.12.2.03,IP 3
,123.12.2.04,IP 4
,123.12.2.05,IP 5
,123.12.2.06,IP 6
,123.12.2.07,IP 7
,123.12.2.08,IP 8

...and only save this stuff for each entry:
ab00001,123.12.2.01,123.12.2.09


0

Response Number 4
Name: Judago
Date: October 7, 2008 at 23:50:06 Pacific
Reply:

It can be removed easily, it even makes the script smaller.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1,2 DELIMS=," %%G IN (YOURTEXTFILE.TXT) DO (
CMD /C "ECHO %%G"^|FINDSTR /I /R AB[0-9][0-9][0-9][0-9][0-9]>NUL
IF !ERRORLEVEL!==0 SET LIST=%%G,%%H
CMD /C "ECHO %%H"^|FINDSTR /I LAST>NUL
IF !ERRORLEVEL!==0 ECHO !LIST!,%%G>>NEWFILE.TXT
)


0

Response Number 5
Name: keridbey
Date: October 8, 2008 at 06:12:06 Pacific
Reply:

That is awesome - it works perfectly! I'm a fairly decent batch programmer, but I've never gotten into the details of what the "SETLOCAL ENABLEDELAYEDEXPANSION" can do for you. I've definitely got to do some research on the subject; I'm amazed at how simple the code turned out to be!

Thanks a bunch!!!


0

Related Posts

See More



Response Number 6
Name: keridbey
Date: October 8, 2008 at 07:48:50 Pacific
Reply:

I've been tasked to compare the output that we've got with IP addresses in our DNS table to ensure that what is in the comma-delimited file matches DNS. I know how to grab the IP addresses I need from the NSLOOKUP command into separate files; the trick is to match them up properly to each other. Essentially, to have the same format as the original output so a file comparison can be done (i.e. ab00001,123.12.2.01,123.12.2.09). I'm trying to pick apart your script to see if I can extrapolate from it, but so far it's just not clicking. Any thoughts on this?


0

Response Number 7
Name: Judago
Date: October 9, 2008 at 01:53:11 Pacific
Reply:

I'm not sure quite what your trying to do, make a new file with the same format or compare another file with the same format.


What's the format of the data? Will it be identical to the other data (ab00001,123.12.2.01,123.12.2.09) or will it just ba a single address(123.12.2.01), the range (123.12.2.01,123.12.2.09) or something else.

If your trying match data in the same format, the range only or one adress without comparing the second at all you could try making a couple of temporary copies of your files to reduce down to find what doesn't match. I think this would make things easier because instead of knowing what's right you will know what's wrong.


@ECHO OFF
for /f "delims=" %%g in (oneofthefiles.txt) do (
findstr /i /v /c:"%%g" theothertextfile.txt>tmp
copy /y tmp theothertextfile.txt
)

The script above matches exact lines and outputs the rest of the file into itsself(via a temp file), it would need to be run twice with the positions of the files reversed to find the differences between the files. If you only have the range or one address then you will need to specify tokens.

If both files should contain the exact same data you could possibly use fc which would be lots faster, if the files are in a differ order fc will find lots of differences so use sort on them first.


0

Response Number 8
Name: keridbey
Date: October 9, 2008 at 13:06:04 Pacific
Reply:

The original CSV file was from a spreadsheet that was supposed to show what had been entered into our DNS. There have been some questions about whether or not what was keyed into the spreadsheet is what really got entered into DNS.

We've got the output from your script. In DNS, the first IP address should be assigned to the DNS name "abXXXXX" and the second IP should be assigned to "abXXXXXr01." I can grab the IPs from DNS with the NSLOOKUP command, but I'm not sure how to concatenate the DNS name with the two IP addresses in one comma-delimited file (so that I can then run the FC command on both CSV files). In a perfect world, both CSV files will end up identical.

Hopefully this all makes some kind of sense. :)


0

Response Number 9
Name: Judago
Date: October 9, 2008 at 14:23:42 Pacific
Reply:

Now I think I know what you mean and an idea on how to do it, input the first ip to nslookup, output the server name after being edited and vica versa. I don't have time just now(maybe tonight).

You might want to consider starting a new thread so you get the benefits of having a few different minds working on it; I'm not sure how many people read threads this far down. I'll definitely give it a after work.


0

Response Number 10
Name: Judago
Date: October 10, 2008 at 01:39:17 Pacific
Reply:

I think I may hove gotten back to this before you have, I've written a new script but I can't be sure it will work the way you want because I only have my modem to test on and I don't normally work with dns or nslookup.

Here it is anyway:


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET SW0=0
SET SW1=0
FOR /F "TOKENS=1,2 DELIMS=," %%A IN (TEXTFILE.TXT) DO (
FOR /F "TOKENS=2 DELIMS= " %%C IN ('NSLOOKUP %%B^|FINDSTR /I NAME:') DO SET LIST2=%%C
FOR /F "TOKENS=2 DELIMS= " %%D IN ('NSLOOKUP %%A^|FINDSTR /I ADDRESS:') DO IF !SW0!==0 SET LIST2=!LIST2!,%%D&&SET SW0=1
FOR /F "TOKENS=2 DELIMS= " %%E IN ('NSLOOKUP %%AR01^|FINDSTR /I ADDRESS:') DO IF !SW1!==0 SET LIST2=!LIST2!,%%E&&SET SW1=1
ECHO !LIST2!>>NEWTEXTFILE.TXT
SET SW0=0
SET SW1=0
)


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Nested FOR Loop

Nested FOR loop prevents parent from complete www.computing.net/answers/programming/nested-for-loop-prevents-parent-from-complete/20042.html

Batch file nested FOR loops help www.computing.net/answers/programming/batch-file-nested-for-loops-help/15196.html

using for loops to interate unicode www.computing.net/answers/programming/using-for-loops-to-interate-unicode/12858.html