Community
    • Login

    Can I sort IP addresses in numeric value

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    20 Posts 12 Posters 13.8k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • michalpl7M Offline
      michalpl7 @Coises
      last edited by

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • michalpl7M Offline
        michalpl7 @PeterJones
        last edited by

        @PeterJones hello the problem with this command is that it also could change MAC address deletes “00” from it.

        PeterJonesP 1 Reply Last reply Reply Quote 0
        • michalpl7M Offline
          michalpl7 @Coises
          last edited by

          @Coises said in Can I sort IP addresses in numeric value:

          1,2,3,4

          Perfect! Thanks :)

          1 Reply Last reply Reply Quote 0
          • PeterJonesP Offline
            PeterJones @michalpl7
            last edited by

            @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.

            1 Reply Last reply Reply Quote 1
            • JonJ Offline
              Jon
              last edited by

              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-notation

              Those 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()
              
              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA Offline
                Alan Kilborn @Jon
                last edited by

                @Jon

                This script will, perhaps quietly, corrupt a user’s file, because it changes line endings from Windows’ type (CRLF) to Linux type (LF). :-(

                Mark OlsonM 1 Reply Last reply Reply Quote 1
                • Mark OlsonM Offline
                  Mark Olson @Alan Kilborn
                  last edited by Mark Olson

                  @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()]
                  
                  Alan KilbornA 1 Reply Last reply Reply Quote 4
                  • Alan KilbornA Offline
                    Alan Kilborn @Mark Olson
                    last edited by Alan Kilborn

                    @Mark-Olson

                    My version of that is eol = ['\r\n', '\n', '\r'][editor.getEOLMode()]… the same, but all in one place.

                    1 Reply Last reply Reply Quote 3
                    • Alan KilbornA Offline
                      Alan Kilborn
                      last edited by Alan Kilborn

                      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.

                      1 Reply Last reply Reply Quote 2
                      • supasillyassS Offline
                        supasillyass @guy038
                        last edited by supasillyass

                        @guy038 Edit > Line Operations > Sort Lines As Integers Ascending 😉

                        Oh wow, necro thread 💀

                        1 Reply Last reply Reply Quote 1

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • First post
                          Last post
                        The Community of users of the Notepad++ text editor.
                        Powered by NodeBB | Contributors