title: How do I define a range and sort by number in Vim using regular expressions?
body: <p>This post is a two-parter.  I'm trying to sort a set of ip statements that look like:</p>
  
  <pre><code>ifconfig em0 alias 172.16.80.1/28
  ifconfig em0 alias 172.16.180.1/32
  ...
  ifconfig em0 alias 172.16.1.1/32
  </code></pre>
  
  <p>by ip.  Is it possible to return a range by using a regular expression?  The following returns an error </p>
  
  <pre><code>%/172.*/sort n
  </code></pre>
  
  <p>and this doesn't (apparently) do anything:</p>
  
  <pre><code>g/172.*/sort n
  </code></pre>
  
  <p>Can this even be done?  </p>
  
  <p>Now, I solved the range problem directly:</p>
  
  <pre><code>18,31 sort
  </code></pre>
  
  <p>but this sorts in ASCII-order, not numerically (wrt the ips).</p>
  
  <pre><code>ifconfig em0 alias 172.16.180.1/32
  ...
  ifconfig em0 alias 172.16.80.1/28
  </code></pre>
  
  <p>and unfortunately this <a href="http://vim.wikia.com/wiki/Sort_lines" rel="nofollow noreferrer">Vim Tip</a> tip doesn't work:</p>
  
  <pre><code>18,31 sort n
  </code></pre>
  
  <p>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?</p>
  
  <p>UPDATE
  The following works:</p>
  
  <pre><code>18,31 !sort -n -t . -k 3,3 -k 4,4
  </code></pre>
  
  <p>(I only need to sort on the last two two bytes.)</p>