Can I sort IP addresses in numeric value
-
Works great. Tiny addition: make sure you have the correct EOL. I replaced the comma’s in a comma-separated list of IP addresses with ‘\n’ in a CRLF (Windows) document. Sorting as described above produced unpredictable (to me) results.
-
@Matthijs-Wensveen said:
I replaced the comma’s in a comma-separated list of IP addresses with ‘\n’ in a CRLF (Windows) document
For the benefit of any future readers, what you should have done is a regular-expression replacement with
\r\n
for a Windows document.Sorting as described above produced unpredictable (to me) results.
What does this mean? You also said “Works great”–so which is it?
-
Hello is there any option to sort ip addresses if they followed by other data in columns? This method is not working. For example:
IP MAC
192.168.1.140 0023-ac20-3918
192.168.1.49 08ea-2931-ca12
192.168.1.145 08ea-2903-bc32
192.168.1.133 98f1-12ca-2456
192.168.1.73 9440-21ab-2512
192.168.1.134 eceb-565a-2953
192.168.1.132 d067-bc22-3174 -
@michalpl7 said in Can I sort IP addresses in numeric value:
Hello is there any option to sort ip addresses if they followed by other data in columns
@guy038’s regex above assumed the whole line was an IP.
But with a slight tweak – allowing a space or tab to come after the final digits, not just a dot or newline – makes it match any of your examples. So his first FIND WHAT becomes
(?:^|(?<=\.))\d(\d)?(?=\.|\h|$)
His first replacement (with spaces) works… but it’s harder to undo later when you have other spaces in the rest of your line. So I change his first REPLACE WITH to
0(?1:0)$0
so that it inserts one or two zeroes instead of one or two spacesSo
FIND =(?:^|(?<=\.))\d(\d)?(?=\.|\h|$)
REPLACE =0(?1:0)$0
SEARCH MODE = regular expressionThat search/replace will give you
192.168.001.140 0023-ac20-3918 192.168.001.049 08ea-2931-ca12 192.168.001.145 08ea-2903-bc32 192.168.001.133 98f1-12ca-2456 192.168.001.073 9440-21ab-2512 192.168.001.134 eceb-565a-2953 192.168.001.132 d067-bc22-3174
Now you can sort lexicographically ascending as he recommended.
Then you need to change the second search/replace. Instead of searching for spaces and removing them, what we want to do is search for leading zeroes and remove them.
FIND =
\b0+(?=\d+?(?=\.|\h))
REPLACE = leave empty
SEARCH MODE = regular expressionThis got me to
192.168.1.49 08ea-2931-ca12 192.168.1.73 9440-21ab-2512 192.168.1.132 d067-bc22-3174 192.168.1.133 98f1-12ca-2456 192.168.1.134 eceb-565a-2953 192.168.1.140 0023-ac20-3918 192.168.1.145 08ea-2903-bc32
-
@michalpl7 said in Can I sort IP addresses in numeric value:
Hello is there any option to sort ip addresses if they followed by other data in columns? This method is not working. For example:
IP MAC
192.168.1.140 0023-ac20-3918
192.168.1.49 08ea-2931-ca12
192.168.1.145 08ea-2903-bc32
192.168.1.133 98f1-12ca-2456
192.168.1.73 9440-21ab-2512
192.168.1.134 eceb-565a-2953
192.168.1.132 d067-bc22-3174In addition to Peter Jones’ solution, you can also do this in a single operation with the Columns++ plugin.
Select the lines you want to sort.
Select Sort… from the Columns++ menu.
Select:
What to sort: Whole lines
Sort type: Ascending and Numeric
Sort key: Regular expressionCheck: Specify keys using capture groups.
Enter:
Find what:(\d+)\.(\d+)\.(\d+)\.(\d+)
Keys:1,2,3,4
Click OK.
When asked to “Convert to a rectangular selection enclosing the selected lines?” click OK.
-
This post is deleted! -
@PeterJones hello the problem with this command is that it also could change MAC address deletes “00” from it.
-
-
@michalpl7 said in Can I sort IP addresses in numeric value:
it also could change MAC address deletes “00” from it
I was afraid you were going to realize that exception.
(?:^|\.)\K0+(?=\d+?(?=\.|\h))
This changes the requirement to whatever goes before the leading zero digits must be either start of line or a period. But because of the
\K
, you have to use Replace All (it will not work with a single Replace)When I tried my old one on
192.168.001.020 d067-bc22-0004
it wrongly became
192.168.1.20 d067-bc22-4
but when I tried my modified expression, it correctly became
192.168.1.20 d067-bc22-0004
But if @Coises’s plugin solution works for you, I’d go that way, because it doesn’t involve as much complication, and ensures that it’s only dealing with the digits inside an IP address.
-
The Python solution hasn’t been discussed here and as that thread is very old I’ll post here.
… I need 1 reputation point to post links, so here it is - remove the spaces!
https:// community.notepad-plus-plus .org/topic/11105/feature-request-sort-by-ip-address-cidr-notationThose scripts didn’t work for me so I wrote my own. This handles both IP & CIDR interchangeably.
import re addresses = editor.getText().split('\n') # Contents to string array non_blank_addresses = [] # Filter out blank lines for addr in addresses: # and clean the input data using regular expressions. cleaned_addr = re.sub(r'\s', '', addr) # Remove whitespace if cleaned_addr: # Check if not empty non_blank_addresses.append(cleaned_addr) # Sort all addresses (CIDRs and individual IPs) sorted_addresses = sorted(non_blank_addresses, key=lambda addr: ( tuple(map(int, re.split(r'[/.]', addr)))[:-1], # Extract IP components int(re.split(r'[/.]', addr)[-1]) if '/' in addr else 32 # Extract and convert prefix length )) editor.beginUndoAction() editor.setText('\n'.join(sorted_addresses)) editor.endUndoAction()
-
This script will, perhaps quietly, corrupt a user’s file, because it changes line endings from Windows’ type (CRLF) to Linux type (LF). :-(
-
@Alan-Kilborn said in Can I sort IP addresses in numeric value:
corrupt a user’s file, because it changes line endings from Windows’ type (CRLF) to Linux type (LF).
AlanKilborn is correct.
My practice in any file when I’m dumping lines is to do something like this:
# Near the top of the script (with other global constants) EOLS = ('\r\n', '\r', '\n') # code #... # whenever I want to choose newline, do this: eol = EOLS[editor.getEOLMode()]
-
My version of that is
eol = ['\r\n', '\n', '\r'][editor.getEOLMode()]
… the same, but all in one place. -
I spoke before of:
perhaps quietly, corrupt a user’s file
and then I presented some code which does just that. :-(
Instead of:
eol = ['\r\n', '\n', '\r'][editor.getEOLMode()]
in my posting immediately above, it should have been:
eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
(note that the
'\r'
and the'\n'
were swapped in the erroneous code)My apologies for the error.
-
@guy038 Edit > Line Operations > Sort Lines As Integers Ascending 😉
Oh wow, necro thread 💀