Sort a list of IP's

Find, replace, find in files, replace in files, regular expressions

Sort a list of IP's

Postby IccMyself » Fri Jun 17, 2005 10:31 am

How can I sort a list of IP's with UltraEdit ?

I can sort a list of IP's in Unix by
sort -n -t . +0 -1 +1 -2 +2 -3 +3 -4 <file>

UE can sort the list but it puts them in
nuermical order.. such as...

191.116.33.195
191.17.3.241
191.17.71.242
191.222.0.106 which should be..

191.17.3.241
191.17.71.242
191.116.33.195

I have the 4 keys but they are not in columns but seperated
by .
191.222.0.106
User avatar
IccMyself
Newbie
 
Posts: 3
Joined: Wed Aug 24, 2005 11:00 pm

Re: Sort a list of IP's

Postby Mofi » Sat Jun 18, 2005 7:58 am

Quick and dirty macro, surely not the best solution. The macro expands all values to 3 numbers by adding the missing leading zeros, sort it and removes the leadings zeros. Remove the red lines at the end of the macro, if you don't want to remove the added zeros. The file should contain only the IP addresses and every IP address should beginn at start of a line. The regexps are in UltraEdit style!

InsertMode
ColumnModeOff
HexOff
UnixReOff
Bottom
IfColNum 1
Else
"
"
EndIf

Top
TrimTrailingSpaces
Find RegExp "%^([0-9]^)."
Replace All "00^1."
Top
Find RegExp "%^([0-9][0-9]^)."
Replace All "0^1."
Loop
Top
Find RegExp ".^([0-9]^)."
IfFound
Find RegExp ".^([0-9]^)."
Replace All ".00^1."
Else
ExitLoop
EndIf
EndLoop
Loop
Top
Find RegExp ".^([0-9][0-9]^)."
IfFound
Find RegExp ".^([0-9][0-9]^)."
Replace All ".0^1."
Else
ExitLoop
EndIf
EndLoop
Top
Find RegExp ".^([0-9]^)$"
Replace All ".00^1"
Top
Find RegExp ".^([0-9][0-9]^)$"
Replace All ".0^1"
SortAsc IgnoreCase RemoveDup 1 -1 0 0 0 0 0 0
Top
Find RegExp "0^([0-9][0-9]^)"
Replace All "^1"
Top
Find RegExp "0^([0-9]^)"
Replace All "^1"
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Sort a list of IP's

Postby IccMyself » Mon Jun 20, 2005 9:19 pm

Mofi wrote:Quick and dirty macro, surely not the best solution. The macro expands all values to 3 numbers by adding the missing leading zeros, sort it and removes the leadings zeros. Remove the red lines at the end of the macro, if you don't want to remove the added zeros. The file should contain only the IP addresses and every IP address should beginn at start of a line. The regexps are in UltraEdit style!


Doesn't seem to work.
Do I need to select the lines beginning with an IP ?

I get an error..
"Search string not found!"

It seems to fail at line 7..
Find RegExp "%^([0-9]^)."

UE needs a MACRO debugger or at the very least a way to step thru
the macro one line at a time...
User avatar
IccMyself
Newbie
 
Posts: 3
Joined: Wed Aug 24, 2005 11:00 pm

Re: Sort a list of IP's

Postby Mofi » Tue Jun 21, 2005 2:15 am

The macro is written for a file which only contains IP addresses at beginning of the line. So copy the addresses temporarily to a new file. No need to save the new file. And you have to check the macro option Continue if a Find with Replace not found at the macro properties.

For "debugging" simply insert the ExitMacro command, where the macro should stop so you can look the result of the macro execution til this point. Use "Revert to Saved" from File menu to undo all changes, edit the macro to move the ExitMacro to a new position or delete it and run macro again.

Sometimes it is even better to insert following commands, where you want to pause the execution of the macro:

GetString "Break 1"
Key BACKSPACE

Now the macro shows a dialog, where the user can enter a string, which is inserted into the file. When the dialog is opened, look at your file, then enter a single character (1 space for example) and press OK. The inserted single character is immediately deleted with backspace and the macro continues until next GetString command. "Break 1" is a text, which is displayed above the enter field, so you can define different "break points".

This "pause" method can be expanded with following commands:

GetString "Break 1"
Key LEFT ARROW
IfCharIs "1"
Key DEL
ExitMacro
Else
Key DEL
EndIf

This gives you the possibilitiy to stop macro execution, when you enter only the character '1' as string in the GetString dialog, all other entered single characters continue macro execution.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Sort a list of IP's

Postby IccMyself » Fri Jun 24, 2005 8:30 pm

It seems I need to run the macro more than once to get
the file sorted.

I created an excel spreadsheet to generate a list of random ip's

A1 =INT(RAND()*(255-1)+1)
B1 =INT(RAND()*(255-0)+0)
C1 =INT(RAND()*(255-0)+0)
D1 =INT(RAND()*(255-0)+0)
E1 =A1&"."&B1&"."&C1&"."&D1

I take the E column and dump it into UE.

152.54.108.100
183.75.176.103
231.106.225.241
59.62.210.126
131.26.17.43
81.163.117.222
73.87.217.1
176.223.122.213
17.242.89.182
210.15.137.2
30.231.95.178
39.108.69.5
31.156.150.84
44.121.62.178
88.188.227.92
119.77.60.42
240.239.80.72
233.133.173.100
134.207.176.17
41.232.141.5
195.13.175.36
114.54.47.196
184.149.13.176
75.130.247.174
83.57.161.132
131.8.48.42
248.17.96.45
55.157.228.87
131.64.201.243
169.11.95.1
23.224.121.158
111.35.136.121
191.92.83.197
4.65.23.198
224.183.84.54
203.35.30.138

After sorting 23.35.30.138 will appear after
195.13.175.36 instead of 17.242.89.182

Sorting it 1 more time, and its sorted :)
User avatar
IccMyself
Newbie
 
Posts: 3
Joined: Wed Aug 24, 2005 11:00 pm

Re: Sort a list of IP's

Postby Mofi » Sat Jun 25, 2005 9:30 am

Although 23.35.30.138 is not at your list, I have found the problem. I guess 23.35.30.138 was the last IP address and this last line was not terminated with a valid line ending (CRLF for DOS or LF for Unix).

I have added the green marked macro code, which adds a line termination on last line of the file, if it is not already present. Then the sort works with only one run.

Note: If you normally work with Unix regular expressions, add UnixReOn as last command to the macro code or this macro will also turn off your prefered setting of Unix style Regular Expressions at Advanced - Configuration - Find.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions