Sorting similar text lines

February 26, 2009 at 09:08:17
Specs: Windows XP, 1.3 Ghz / 1 Gb RAM
A couple of years ago I was trying to parse and sort this:

192.168.0.1 08-00-0e-3d-19-90 dynamic
192.168.0.2 08-00-0e-3d-19-fe dynamic
192.168.0.3 08-00-0e-3d-19-5c dynamic
192.168.0.4 08-00-0e-3d-1a-13 dynamic
192.168.0.25 00-15-70-73-20-38 dynamic
192.168.0.35 00-16-e0-7c-43-80 dynamic
192.168.0.61 00-d0-cf-05-68-01 dynamic
192.168.0.253 00-d0-cf-05-68-01 dynamic
192.168.0.252 00-0D-56-28-70-9D

Unit 1 Port 1 08-00-0e-3d-1a-13 1 No
Unit 1 Port 2 08-00-0e-3d-19-fe 1 No
Unit 1 Port 3 08-00-0e-3d-19-5c 1 No
Unit 1 Port 4 08-00-0e-3d-19-90 1 No
Unit 1 Port 9 00-d0-cf-05-68-01 1 No
Unit 1 Port 10 00-0d-56-28-70-9d 1 No
Unit 1 Port 18 00-15-70-73-20-38 1 No

into this:

192.168.0.1 Port 4
192.168.0.2 Port 2
192.168.0.3 Port 3
192.168.0.4 Port 1
192.168.0.25 Port 18
...
...
...and so on.

IVO came up with a brilliant tiny bit of coding that took care of it for me:

@Echo Off>IPTable.txt
For /F "tokens=1,2" %%a in ('Type ARP.txt^|Find /V "Unit"') Do (
For /F "tokens=3,4" %%i in ('Type ARP.txt^|Find "Unit"^|Find "%%b"') Do (
Echo %%i %%j %%a>>IPTable.txt
))

I was a little confused by how IVO chose the individual letters for the variables, but it worked nonetheless. Now I have a different style of information to sort in the same general way, but I've not been able to adapt IVO's code to fit the situation. Here's the info to be sorted:

0030-18a5-23c5 1 Learned Ethernet1/0/11 AGING
0800-0e3b-061a 1 Learned Ethernet1/0/2 AGING
0800-0e3b-06b9 1 Learned Ethernet1/0/3 AGING
0800-0e3b-0692 1 Learned Ethernet1/0/4 AGING
0800-0e45-1fe3 1 Learned Ethernet1/0/5 AGING
0800-0e3b-06bf 1 Learned Ethernet1/0/6 AGING
0015-5827-a4ba 1 Learned Ethernet1/0/7 AGING
0011-252e-5cb6 1 Learned Ethernet1/0/8 AGING
0008-742e-3014 1 Learned Ethernet1/0/9 AGING
00d0-cf05-940c 1 Learned Ethernet1/0/10 AGING
0030-18a5-23c5 1 Learned Ethernet1/0/11 AGING
0015-7073-3b00 1 Learned Ethernet1/0/14 AGING

Eth1/0/1 UP A10M Ahalf access 1
Eth1/0/2 UP A10M Ahalf access 1
Eth1/0/3 UP A10M Ahalf access 1
Eth1/0/4 UP A10M Ahalf access 1
Eth1/0/5 UP A10M Ahalf access 1
Eth1/0/6 UP A10M Ahalf access 1
Eth1/0/7 UP A100M Afull access 1
Eth1/0/8 UP A100M Afull access 1
Eth1/0/9 UP A10M Ahalf access 1
Eth1/0/10 UP 10M half access 1
Eth1/0/11 UP A100M Afull access 1
Eth1/0/12 UP A100M Afull access 1
Eth1/0/13 DOWN A A access 1
Eth1/0/14 UP A10M Ahalf access 1
Eth1/0/15 DOWN A A access 1
Eth1/0/16 DOWN A A access 1
Eth1/0/17 DOWN A A access 1
Eth1/0/18 DOWN A A access 1
Eth1/0/19 DOWN A A access 1
Eth1/0/20 DOWN A A access 1
Eth1/0/21 DOWN A A access 1
Eth1/0/22 DOWN A A access 1
Eth1/0/23 DOWN A A access 1
Eth1/0/24 DOWN A A access 1

I'd like to just put the MAC address for the device in the second section either next to "access 1" or in place of "access 1" on the line with the matching port number (the "1/0/1" with the third number being the one to match). Any ideas where to go with this?


See More: Sorting similar text lines

Report •


#1
February 26, 2009 at 12:05:37
IVO usually does. I'm too tired to get through it now but the choice of letters is arbitrary. Just don't use the same set in 2 nested loops.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
February 27, 2009 at 05:49:51
I found a program called XCHANGE.EXE that can find/replace text in a text file, so I used it to trim Ethernet down to Eth, then applied this code:

For /F "tokens=1,4" %%a in ('Type %TempDir%\IPTable.txt^|Find /v "access"') Do (For /F "tokens=1,2,3,4" %%i in ('Type %TempDir%\IPTable.txt^|Find /i "access"^|Find /i "%%b "') Do (Echo %%b %%j %%k %%l %%a>>%TempDir%\NewIPTable.txt))

That seems to work! I've not had any classes on programming, so it took a little while of staring at IVO's code for me to understand that it's actually really simple, just longer than the average line. :)


Report •

Related Solutions


Ask Question