Computing.Net > Forums > Programming > Sorting similar text lines

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.

Sorting similar text lines

Reply to Message Icon

Name: keridbey
Date: February 26, 2009 at 09:08:17 Pacific
OS: Windows XP
CPU/Ram: 1.3 Ghz / 1 Gb RAM
Subcategory: Batch
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: February 26, 2009 at 12:05:37 Pacific
Reply:

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


0

Response Number 2
Name: keridbey
Date: February 27, 2009 at 05:49:51 Pacific
Reply:

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. :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: Sorting similar text lines

Parsing/sorting similar text lines www.computing.net/answers/programming/parsingsorting-similar-text-lines/15999.html

Batch to copy specific text lines www.computing.net/answers/programming/batch-to-copy-specific-text-lines/15394.html

Sort a text file in VBScript www.computing.net/answers/programming/sort-a-text-file-in-vbscript/7334.html