Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 997723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:04:48+00:00 2026-05-16T07:04:48+00:00

This post is a two-parter. I’m trying to sort a set of ip statements

  • 0

This post is a two-parter. I’m trying to sort a set of ip statements that look like:

ifconfig em0 alias 172.16.80.1/28
ifconfig em0 alias 172.16.180.1/32
...
ifconfig em0 alias 172.16.1.1/32

by ip. Is it possible to return a range by using a regular expression? The following returns an error

%/172.*/sort n

and this doesn’t (apparently) do anything:

g/172.*/sort n

Can this even be done?

Now, I solved the range problem directly:

18,31 sort

but this sorts in ASCII-order, not numerically (wrt the ips).

ifconfig em0 alias 172.16.180.1/32
...
ifconfig em0 alias 172.16.80.1/28

and unfortunately this Vim Tip tip doesn’t work:

18,31 sort n

In fact, it does nothing; sorting on the original list leaves the original order intact. So even if returning a range via regular expressions is impossible, how do I sort these lines numerically?

UPDATE
The following works:

18,31 !sort -n -t . -k 3,3 -k 4,4

(I only need to sort on the last two two bytes.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:04:49+00:00Added an answer on May 16, 2026 at 7:04 am

    You can specify a regex for the beginning line and a regex for the ending line of a range. If you had this file:

    foo
    bar
    ifconfig em0 alias 172.16.80.1/28
    ifconfig em0 alias 172.16.55.1/28
    ifconfig em0 alias 172.16.180.1/28
    ifconfig em0 alias 172.16.1.1/32
    baz
    quux
    

    You could sort the lines with IP addresses like this:

    :/172/,/baz/-1sort
    

    This says “start at the first line that matches /172/, end one line above the first line that matches /baz/“. You might come up with a more clever regex depending upon your file’s contents.

    I don’t know how to sort IP addresses in Vim in one pass. But if you have access to GNU sort, you could do it something like this (as per this article):

    :/172/,/baz/-1!sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4
    

    That’ll sort them numerically. Not sure what you mean by “lexicographically” with regards to IP addresses.

    Regarding :sort and :g, the Vim help at :h :sort says:

    Note that using “:sort” with “:global” doesn’t sort the matching lines, it’s quite useless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this post is a long one sorry for that but the problem is complex
I am trying to create a WCF Streaming Service. I have two requirements that
This post is actually divided in two questions: Which kinds of associations classes/interfaces usually
As is asked in this post , there are two ways to call another
This is homework so for those that dont like looking at someone asking questions
I need to center a small amount of text between two images. This post
I read this post and I really like this solution to templating but I
I was looking at this post that describes a simple way to do databinding
The solution #2 (dynamic) in this question/answer post: overlay two images in android to
I came across this post , which reports the following interview question: Given two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.