Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a list by digits appearing after trash digits in VIM

Tags:

regex

vim

sorting

I need to sort and diff a few lists that have 1,000s of entries on them. The lists looks like:

11-2-3049_2007_squib.pdf
11-11-5476_2004_squib.pdf
13-2-5477_2006_squib.pdf
14-3-3045A_2004_squib.pdf
14-CMF-3046_2004_squib.pdf
14-2-3047_2005_squib.pdf
14-4-3048_2004_squib.pdf
15-7-3050P_2004_squib.pdf

I'm looking to sort by the number between the second - and before the _, such as 3049 in the first example.

I've not been able to combine the sort by column and regex stuff with any success. What do you all suggest?

like image 807
CRS Avatar asked Oct 05 '12 18:10

CRS


2 Answers

This should do the trick:

:sort r  /\v^(.{-}-){2}\zs.{-}\ze_/

See

:help sort

for background here

The regex contains a few twists and turns:

  • \v to engage very magic mode (reducing the need for escaping)
  • \zs and \ze to mark the begin and end of the actual match result
  • {-} to perform non-greedy kleene-star match (in Perl notation, .{-} would be .*?)
like image 134
sehe Avatar answered Nov 09 '22 23:11

sehe


Use the external sort program:

:%!sort -n -t- -k3,3
like image 44
William Pursell Avatar answered Nov 09 '22 23:11

William Pursell